PHP User Login Demo

These PHP scripts demonstration a way to implement a user login area for a PHP based web site. This login mechanism has the following features:

Click here for a demo of it working.

Click here to download the files (tgz).

Files

These scripts are implemented in a very modular fashion to reduce the number of variables that would have to be changed to implement for your site, which means that there is more than you might initially think.

Here are all the files that are involved.

globals.php

This script contains a selection of global variables for use across all the scripts.

View this file

dbconn.php

This script gives us a connexion to the database, in this case we are using MySQL.

View this file

login.php

The main script which checks user name and password entered in login.xhtml, validates it against values in database, creates a session key and sets this both in the database against the users record, and in a cookie. Also handles logout by invalidating cookie.

View this file

login.xhtml

HTML template for the login page, that's all it is! Calls login.php.

View this file

authen.php

This file checks the login and password entered by the user and makes sure that they match the value set in the cookie and that this in turn matches the database. Would be included in any page needing authentication before viewing.

View this file

index.php

A home page with nothing on it, shows the use of the file authen.php to validate the user.

View this file

error.xhtml

Generic HTML page template for displaying error messages, takes $task and $error parameters.

View this file

create_users.sql

And finally a bit of SQL for creating a table called 'users' with some suggested fields for storing their information.

View this file