LHCb/CBPF web page >> Tutorials >> How to create a restricted area in 5 minutes
user1:encryptedpassword user2:encryptedpassword user3:encryptedpasswordFor encrypting the password it is possible to use the Apache tools from command-line, using the following syntax:
htpasswd –c /full_and_absolute_path/.htpasswd user1Once given this command, you will be asked to insert the password twice. Later on the .htpasswd file is going to be modified by the adding of user1 with the respective password correctly encrypted. Or (a lot easier) use one of the many online tools, this one for example: http://www.4webhelp.net/us/password.php If you are working with Apache on Windows platform you have to obviously use the Apache tool, otherwise it won’t function. Now save your file as .htpasswd (no name – htpasswd extension) and position it in a folder secure and unreadable by the web server. In our case, it can very well be the same folder we intend to protect.
The result might be something like this: /home/virtual/virtual-site95/var/www/html/ This is the position of your public folder. We need to know this path since we have to indicate with precision where the .htpasswd file is situated.<
?php
echo $_SERVER["DOCUMENT_ROOT"];
?>
AuthUserFile /home/virtual/virtual-site95/var/www/html/folder_to_protect/.htpasswd AuthName RestrictedArea AuthType Basic require user user1 require user user2If you already have a .htaccess file in the folder, you will simply add these directives as contained in the file. As you can see, it is necessary to indicate with AuthUserFile the path of the .htpasswd file, which we have positioned in the folder we intend to protect, thus the same in which we will save the .htaccess file. With AuthName we can establish a message which will appear in the login window. Finally in the last line we tell that in this area are admitted the users user1 and user2. Even if user3 provided the correct password, it wouldn’t be accepted. In this way we can use the same .htpasswd file for various restricted areas, and by means of the .htaccess file define for example that in the folder “x” everybody can enter, while in the folder “y” only user1 and user2. Naturally the folder “x” and the folder “y” will have inside a different .htaccess file. In case we want to authorize all users present in the .htpasswd file, enough writing in the last line of the .htaccess file:
require valid-user