Page 1 of 1

PHP getting in to a password protected directory

Posted: Thu Jul 13, 2006 11:21 am
by tecktalkcm0391
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?

Posted: Thu Jul 13, 2006 11:54 am
by Ward
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);

Posted: Thu Jul 13, 2006 12:51 pm
by Weirdan
teck, are files you want to get located at the same server with your script?

Posted: Thu Jul 13, 2006 12:52 pm
by Ollie Saunders
password protected how?

Posted: Thu Jul 13, 2006 12:55 pm
by dull1554
i've had a similar problem, im assuming that hes using apache, and hes passwording a dir like myself using .htaccess and .htpasswd

Posted: Thu Jul 13, 2006 6:30 pm
by tecktalkcm0391
dull1554 wrote:i've had a similar problem, im assuming that hes using apache, and hes passwording a dir like myself using .htaccess and .htpasswd
I have a directory password protected by .htpasswd and .htaccess... so how could I unlock it just for PHP

Posted: Thu Jul 13, 2006 6:53 pm
by Chris Corbyn
tecktalkcm0391 wrote:
dull1554 wrote:i've had a similar problem, im assuming that hes using apache, and hes passwording a dir like myself using .htaccess and .htpasswd
I have a directory password protected by .htpasswd and .htaccess... so how could I unlock it just for PHP
That type of password protection only applies to HTTP. PHP reads the filesystem so it won't even be asked to authenticate.

If you're trying to require files over HTTP (for example if they are on another server) then use CURL (see php manual).

Posted: Tue Jul 25, 2006 7:30 pm
by tecktalkcm0391
Can someone please help me with this I do not understand CURL at all...

Posted: Tue Jul 25, 2006 7:32 pm
by tecktalkcm0391
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);

?>

Posted: Tue Jul 25, 2006 7:34 pm
by kbrown3074
There are some password protected files on our web server but I cant for the life of me remember how the hell we did it. When you access the page, a login/pass dialog pops up. I thought we used .htaccess...could be wrong tho.

Posted: Tue Jul 25, 2006 8:30 pm
by tecktalkcm0391
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...

Posted: Tue Jul 25, 2006 8:45 pm
by neophyte
You can just use headers to authenticate with http authentication:

http://us2.php.net/features.http-auth

Posted: Wed Jul 26, 2006 2:23 am
by Weirdan
Would that work to allow me to acess the files in the protected by .htaccess files...
Well... perhaps. Why don't you try it instead of asking?

Posted: Wed Jul 26, 2006 9:38 am
by tecktalkcm0391
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);