PHP getting in to a password protected directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

PHP getting in to a password protected directory

Post 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?
Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Post 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);
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

teck, are files you want to get located at the same server with your script?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

password protected how?
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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).
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Can someone please help me with this I do not understand CURL at all...
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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);

?>
User avatar
kbrown3074
Forum Contributor
Posts: 119
Joined: Thu Jul 20, 2006 1:36 pm

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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...
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

You can just use headers to authenticate with http authentication:

http://us2.php.net/features.http-auth
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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);
Post Reply