Thursday, August 27, 2009

How to protect your web directory using htaccess

First create a .htaccess file inside the directory to protect and add the following codes

AuthName "Restricted Area"
AuthType Basic
AuthUserFile /home/user/projects/.htpasswd
AuthGroupFile /dev/null
require valid-user

Create a file with the name .htpasswd in /home/user/projects/ directory and add the following lines

anish:uFWLc7lvvKExE

This is the username and password (encrypted) to access the protected directory.

To encrypt the password use the following php code:
<?php

// Password to be encrypted for a .htpasswd file
$clearTextPassword = 'admin123';

// Encrypt password
$password = crypt($clearTextPassword, base64_encode($clearTextPassword));

// Print encrypted password
echo $password;
?>

No comments:

Post a Comment