Hi,
In this tutorial, I will show you how to secure a folder using a authentication.
The result will look like that:
First, you have to create a file ".htaccess"
He must contain the following code:
AuthName "Protected Administration Page"
AuthType Basic
AuthUserFile "/real/path/to/the/file/.htpasswd"
Require valid-user
You have to change in the code above is the third line(
AuthUserFile "/real/path/to/the/file/.htpasswd"). You have to find the real path of your folder. There is a php function to get that the real path: realpath
So execute the following code in your web site. Make sure that the file realpath.php is in the folder that you want to secure.
realpath.php
<?php echo realpath('realpath.php'); ?>
By executing the code above, you will get something like:
/home/www/webestools/admin/realpath.php
You only have to change "
realpath.php" by "
.htpasswd"
So the code of your .htaccess file will became:
AuthName "Protected Administration Page"
AuthType Basic
AuthUserFile "/home/www/webestools/admin/.htpasswd"
Require valid-user
After you got the real path, you can delete the realpath.php file.
Now, you have to define the users IDs that will have the access to the folder. You will have to create a file ".htpasswd"
a user ID will be like:
username:cryptedpassword
You can add as many users you want, you only have to add a new line.
To get the encrypted password, you have to use the crypt function of php.
This is a tool to get an encrypted version of you password:
.htpasswd password encryptor
You have to copy the line that you got with the ".htpasswd password encryptor" in the .htpasswd file.
That's it, your folder will be secured, to access it files using the http protocol, a window will be displayed asking you for the IDs.
I recommend you to create the files htaccess.txt and htpasswd.txt in your computer, to send them in your web server and then to rename them as .htaccess and .htpasswd using your FTP software
I hope this tutorial will be useful
Similar Scripts and Tutorials