Protect web directories
|
Author: Mattias (mattias@999tutorials.com) Categories: General www, Apache |
||
| Sometimes you need to put important but secret data on a web site. Then it's important to protect it from other persons! Let's say your web stats, your admin section or just your private photo album ;-) | ||
|
The tutorial: Digg this, Post to del.icio.us, You probably have information and functions on websites that is only for you and your staff. One solutions that works often is using PHP or ASP to verify username and password, but sometimes the easiest way is to protect a complete directory of files. For this you need neither PHP or ASP, you need Apache to use a built in function called "htaccess". Let’s say you want to protect a directory, the first thing you need to do is to create a file named .htaccess in that directory (and other directories to protect). (Very important is the dot in front of the word htaccess) .htaccess needs some things to work with. The first thing is where to find the file with a list of valid usernames and passwords (.htpasswd). Below is a sample of a normal .htaccess file. Just replace /my/path/to with something that goes for your system, but make sure that the directory is outside the web root so no one can surf to that file. This file will contain usernames and encrypted passwords.
AuthUserFile tells .htaccess where to look for the usernames and passwords. Q: But how does apache know who is allowed to access the directory? We have two alternatives for this:
Enter this text for the solutions you choose:
Of course you need to replace “mattias” with your own username... :-) So this is what my .htaccess would look like:
The last thing we have to do is create the password file, .htpasswd. To generate the password you can use PHP’s crypt() function. Simply create a php file with ‘echo crypt(“your password”);’ and you will get your encrypted password for the .htpasswd file. Here is what I did for the username “mattias” and password “guitar”. Important! Once you have created your .htpasswd file you should upload it to a safe directory on your server. It’s very important that the file is not accessible from the web or via public ftp. Make sure the information in .htaccess is correct so apache can find the password file. The final thing is to surf to the directory you protect to see that it’s protected properly. Happy protecting :-) Digg this, Post to del.icio.us, |
