PHP getting in to a password protected directory
Moderator: General Moderators
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
PHP getting in to a password protected directory
I have site.com/folder password protected and I have files that I want php to get to and include. Is there a way to do this?
something like this should work. This will simply loop through a folder (specified by the $folder variable) and include all files found. This won't work if there are other file types in the folder, since they will also be included, and will cause an error.
Code: Select all
$folder = getcwd()."/folder/";
$dir = opendir($folder);
while ($file = readdir($dir))
{
if ($file != "." && $file != "..")
{
$file_path = $folder.$file;
if (file_exists($file_path))
{
include $file_path;
}
}
}
closedir($dir);- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
That type of password protection only applies to HTTP. PHP reads the filesystem so it won't even be asked to authenticate.tecktalkcm0391 wrote:I have a directory password protected by .htpasswd and .htaccess... so how could I unlock it just for PHPdull1554 wrote:i've had a similar problem, im assuming that hes using apache, and hes passwording a dir like myself using .htaccess and .htpasswd
If you're trying to require files over HTTP (for example if they are on another server) then use CURL (see php manual).
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
Ah, I found something would this work?
Code: Select all
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.co.za/protected/recipient.php');
// Username and password, separated by a colon.
curl_setopt($ch, CURLOPT_USERPWD, 'iang:iang');
curl_exec($ch);
curl_close($ch);
?>- kbrown3074
- Forum Contributor
- Posts: 119
- Joined: Thu Jul 20, 2006 1:36 pm
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
tecktalkcm0391 wrote:Ah, I found something would this work?
Code: Select all
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.example.co.za/protected/recipient.php'); // Username and password, separated by a colon. curl_setopt($ch, CURLOPT_USERPWD, 'iang:iang'); curl_exec($ch); curl_close($ch); ?>
Would that work to allow me to acess the files in the protected by .htaccess files...
You can just use headers to authenticate with http authentication:
http://us2.php.net/features.http-auth
http://us2.php.net/features.http-auth
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
I got it thanks.
Code: Select all
// start CURL
$ch = curl_init();
// SET URL
curl_setopt($ch, CURLOPT_URL, 'http://site.com/');
// Username and password, separated by a colon.
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
// Turns curl into a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// set to string
$str = curl_exec($ch);
// close CURL
curl_close($ch);