Password Protecting Directories (Apache)

* These instructions assume you have a folder on your server named ‘administration’.

You can create an administration area in two ways – depending on the needs of the website.

First Method:

You could add a table to the database with username and passwords of allowed users. You would then create an index page inside of the administration folder. This index page will have a form that allows the user to enter name & password. The script will go to the database and try to find the user – if successful; the user is allowed access…otherwise, NOT!

This method is good for multiple users of administration system that have different levels of access.

Second Method:

We will add an .htaccess & .htpasswd file inside of our admin that will limit the users. All users visiting this folder will have the same level of permissions and username/password.

Open textpad.

Paste in the following:

AuthUserFile D:/WAMP/www/administration/.htpasswd
AuthName "Access to Admin"
AuthType Basic

require valid-user

Note: You should change the AuthUserFile Path to whatever your path is. (it may be C:/WAMP/www/admin)

Now visit the following site to generate an .htpasswd file:

.htpasswd generator

It is imperative that you save the files exactly like this: ‘.htaccess’ and ‘.htpasswd’. It looks weird - but this will not work without it.

Save both of these files in your protected directory (admin).

This should now give you a login prompt when you try to go to the folder (Ex: http://localhost/admin)

Filed under: PHP

Leave a Reply