Password protection of web pages

Filed under: htaccess

There are several methods to password protect a web page, some are more secure than others. One method is by using cookies and PHP, an example of this is the php script developed by Zann Marketing, Simple Authorization Script (SAS). This small and simple script allows users to password protect web pages without using a database. This feature of SAS is great for webmasters who do not have access to databases such as MySQL.

Although SAS is quite “secure”, it is not the most secure form of password protection. The most secure form of password protection is through the use of .htaccess and .htpasswd files.

Step by step guide to password protecting web pages.

Your secure pages will have to reside in a directory of its own.

  1. Create an empty directory on your website. For example, you can call it “secure”. The resulting URL would be http://www.yourwebsite.com/secure/
  2. Create a .htpasswd file in the new directory. In this file, add the username and password pairs in the format of:
    username:password

    • You can add multiple usernames and passwords, simply add each new username and password pair on a new line. For example,
      username1:password1
      username2:password2
      username3:password3
  3. Create a .htaccess file in the new directory. In this file, add the lines:
    AuthType Basic
    AuthName "title"
    AuthUserFile /path/yourwebsite.com/secure/.htpasswd
    require valid-user

    • The “title” of the AuthName can be anything you like, for example, “Your Secure Account” or “Authorization Required”.
    • The AuthUserFile is the path of the file .htpasswd. You will have to specify the full path of the .htpasswd file.
  4. Upload your new directory with the 2 new files, .htaccess and .htpasswd.
  5. Test the new secure directory by visiting the page (http://www.yourwebsite.com/secure/) in your web browser, such as Internet Explorer or Firefox.

For any pages you wish to password protect, upload the page into the “secure” directory.

divider

Leave a Comment