Jump to content

[SOLVED] Automatically authenticate for password protected directories with PHP


njdirt

Recommended Posts

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

Link to comment
Share on other sites

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;
}
?>

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.