Simple Access Control

To protect a page, you need to insert PHP code similar to the following at the top of your page:

<? include "BasicAuthenticator.php"; $realm = "My Secret Pages"; $errorMessage = "Private. Keep Out!"; $auth = new BasicAuthenticator($realm, $errorMessage); // Define a set of valid usernames and passwords. $auth->addUser("guest", "secret"); $auth->addUser("anybody", "PassWord"); // And now perform the authentication $auth->authenticate();
...Rest of page goes here...

It's very important that the code appears in your page before any HTML.

The realm parameter contains the name used to describe the pages being protected. If you want to protect several pages using the same set of passwords, they should all be in the same Realm.

The errorMessage parameter contains a message that is displayed to the user if their login fails.

The example above sets up two users, the user guest has the password secret and the user anybody has the password PassWord. You should, of course, tailor the list of usernames and passwords as appropriate. Note that usernames and passwords are case-sensitive.

To see how this works in practice, here is a page that has been protected using the above code. Try it and see - remember, you can log in as guest or anybody using the passwords described above.


Back to main Basic Authenticator page