njdirt Posted August 19, 2008 Share Posted August 19, 2008 Hi, I am working on a script that will be used externally from my site, and needs to access files that are within a password protected folder. I have been looking for a way to automatically enter the username and password (and thus gain temporary access to the files), but I've had no luck searching. Does anyone know how to do this? Is there a safer way? I know that coding the uname and pword into the file is not very secure, but I have no other ideas. Thanks in advance! -Mark Quote Link to comment https://forums.phpfreaks.com/topic/120404-solved-automatically-authenticate-for-password-protected-directories-with-php/ Share on other sites More sharing options...
JonnoTheDev Posted August 19, 2008 Share Posted August 19, 2008 using CURL Quote Link to comment https://forums.phpfreaks.com/topic/120404-solved-automatically-authenticate-for-password-protected-directories-with-php/#findComment-620385 Share on other sites More sharing options...
njdirt Posted August 20, 2008 Author Share Posted August 20, 2008 Thanks, that solves some of the problem! That showed me a safer way to include the page, rather than include();. Let me try to explain my problem in a different way... I have some PHP software where all of the files for a client reside in one folder. To protect them, the file is password protected. Not a PHP password, it's the kind that I set through my FTP client. Now, I am writing some code for the business website, where it needs to access some files in that protected folder. What is the safest way to do this? Thanks in advance! Here is the code I'm going to use for the actual inclusion, using CURL as neil suggested: <?php $curl_handle=curl_init(); curl_setopt($curl_handle,CURLOPT_URL,'http://example.com'); curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); $buffer = curl_exec($curl_handle); curl_close($curl_handle); if (empty($buffer)) { print "Sorry, example.com didn't load.<p>"; } else { print $buffer; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/120404-solved-automatically-authenticate-for-password-protected-directories-with-php/#findComment-621116 Share on other sites More sharing options...
JonnoTheDev Posted August 20, 2008 Share Posted August 20, 2008 Do you mean the directory is protected with .htaccess? If so try: CURLOPT_USERPWD format: "[username]:[password]" Quote Link to comment https://forums.phpfreaks.com/topic/120404-solved-automatically-authenticate-for-password-protected-directories-with-php/#findComment-621123 Share on other sites More sharing options...
njdirt Posted August 20, 2008 Author Share Posted August 20, 2008 Hey, Sorry, yes, I do mean that the directory is protected with .htaccess. I was trying the cURL stuff and did a bunch of research on it. For some reason, though, I still can't get the login part to work. Here's my code: $usrnm = "xxxxx"; $pswrd = "xxxxx"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.mysite.com/protected/file.php'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD,$usrnm . ":" . $pswrd); curl_exec($ch); curl_close($ch); Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/120404-solved-automatically-authenticate-for-password-protected-directories-with-php/#findComment-621326 Share on other sites More sharing options...
JonnoTheDev Posted August 20, 2008 Share Posted August 20, 2008 Try curl_setopt($ch, CURLOPT_USERPWD,"{$usrnm}:{$pswrd}"); Quote Link to comment https://forums.phpfreaks.com/topic/120404-solved-automatically-authenticate-for-password-protected-directories-with-php/#findComment-621511 Share on other sites More sharing options...
njdirt Posted August 22, 2008 Author Share Posted August 22, 2008 Still no luck, even with that last suggestion. Can anyone think of any reason why the server isn't receiving the username and password? Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/120404-solved-automatically-authenticate-for-password-protected-directories-with-php/#findComment-623222 Share on other sites More sharing options...
njdirt Posted August 25, 2008 Author Share Posted August 25, 2008 Hey, Does anybody have any ideas? I am still trying to find something that will work, without luck, unfortunately. please help me finish this! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/120404-solved-automatically-authenticate-for-password-protected-directories-with-php/#findComment-625029 Share on other sites More sharing options...
njdirt Posted August 26, 2008 Author Share Posted August 26, 2008 I just went with a different authentification system for my software. It seems pretty good and very easy!! http://www.zubrag.com/scripts/password-protect.php Quote Link to comment https://forums.phpfreaks.com/topic/120404-solved-automatically-authenticate-for-password-protected-directories-with-php/#findComment-625536 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.