Password protect the sub folder and accessing files on them

Add the following script to the .htaccess file of the subfolder which you wish to password protect

ErrorDocument 401 "Authorisation Required"
AuthUserFile /home/account/public_html/.htpasswd
AuthName "Please Log In"
AuthGroupFile /dev/null
AuthType Basic
Require valid-user

* here the line with ‘AuthUserFile’ gives the full path of the .htpasswd file which contains login details to the subfolder.

http://www.htaccesstools.com/htpasswd-generator/
http://tools.dynamicdrive.com/password/

The above 2 links will help to generate the content for the .htpasswd file

Ok next if we would like access the file inside the password protected folder then use the following sample curl script

$url = "http://www.domain.co.uk/subfolder/script.php";

$username = 'username';
$password = 'password';

$crl = curl_init();
$timeout = 10;
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($crl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt ($crl, CURLOPT_USERPWD, $username . ":" . $password);
$ret = curl_exec($crl);
curl_close($crl);
}

P.S. This script has been tested with cPanel hosting with Apache web server.

Leave a Reply

Your email address will not be published.

eighteen − 6 =