Basic Password Authentication with PHP

Requires PHP on your server.

You may have a page which you do not want the world to see. No matter how careful you are, a dedicated hacker will probably gain access, but you can protect a page (or a whole site) from the average Internet user, by setting up a Username and Password requirement.

Requirements : a small form for user input and a short php routine for verification.

The files are pwd1.htm and pwd2.php3
Both sample files can be downloaded from ftp://ftp.wight.info/pwd
(Right click and 'Copy to folder' in IE or 'Save Link As' in Netscape).

The input form can be used without modification. A simple extra touch is the disguised password input - anybody looking over your shoulder will see stars, not characters.

The php3 file (pwd2.php3) will need the following modifications:-
  • Between <HEAD> and </HEAD> add <base href='http://www.wightindex.com/'> but change it to match your own web site top level. This is necessary to avoid problems with relative addressing of images. If you always use full pathnames for all your graphics, you need not worry about this addition. If you do add it, do not forget the last slash.
    Not including it will cause relatively addressed images to appear 'broken'.
  • $pwd == change from "correct" to your own choice of password
  • $user == change from "fred" to your own choice of username
  • $webpage = change from http://www.wightindex.co.uk to your own
Note: == is double = and only applies to $pwd and $user

Experiment with one page in the first instance, but to protect a whole site you could rename pwd1.htm to index.htm or index.html

It might be a good idea to see it in operation, so follow this link to the form page.
Username fred - Password correct
Entering the correct Username and Password will load up the PHP Menu page. Incorrect entry of either, will produce a failure message.


PHP Menu page