Create Account
Sign In

Secure a folder .htaccess .htpasswd - protect a directory with htaccess and htpasswd


Secure a folder .htaccess .htpasswd - protect a directory with htaccess and htpasswd Secure a folder .htaccess .htpasswd - protect a directory with htaccess and htpasswd The 05/01/2012 at 04:30:21
Rating: 4/5
Keywords: secure a folder http apache athentification administration code htaccess htpasswd auth protect files http php real path secure directory admin

Hi,
In this tutorial, I will show you how to secure a folder using a authentication.

The result will look like that:
Image

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

Custom 404 error pages - php url rewrite ErrorDocument not found htaccess Custom 404 error pages - php url rewrite ErrorDocument not found htaccess PHP Redirection (Header and URL Rewrite) - redirect php page header location RewriteRule PHP Redirection (Header and URL Rewrite) - redirect php page header location RewriteRule NFL Live NFL Live Bootstrap 4 PHP CRUD Bootstrap 4 PHP CRUD Simple PHP Forum Script - php forum easy simple script code download free php forum mysql Simple PHP Forum Script - php forum easy simple script code download free php forum mysql