Explains how to create a secure PHP login script that will allow safe authentication. Features remember-me function using cookies, validates logins on each request to prevent session stealing.
1. How does this work
This is a short explanation why I have chosen these authentication methods.
Users with shell access to the web server can scan valid session id's if the default /tmp directory is used to store the session data.
The protection against this kind of attack is the IP check.
Somebody who has a site (on a shared host with you) can generate valid session for your site.
This is why the checkSession method is used and the session id is recorded in the database.
Somebody may sniff network traffic and catch the cookie.
The IP check should eliminate this problem too.
2. Preparation
You need first to decide what information to store about members, the examples provided will assume almost nothing to make it easier to read.
I will use the PHP 4.1 super global arrays like $_SESSION, $_GET, etc. If you want to make it work on an earlier version of PHP you will have to substitute these with $GLOBALS['HTTP_SESSION_VARS'].
3. Database schema
This is only an example bare structure suitable for online administration, if you want to have registered members you should add more columns.
The schema is somewhat MySQL specific, I have yet to use another database other than MySQL and PostgreSQL but if you are using PostgreSQL you can convert the schema with the example script provided in my article Converting a database schema from MySQL to PostgreSQL.
| CREATE TABLE member (
id int NOT NULL auto_increment, username varchar(20) NOT NULL default '', password char(32) binary NOT NULL default '', cookie char(32) binary NOT NULL default '', session char(32) binary NOT NULL default '', ip varchar(15) binary NOT NULL default '', PRIMARY KEY (id), UNIQUE KEY username (username) ); |
The password and cookie fields are md5 hashes which are always 32 octets long. Cookie is the cookie value that is sent to the user if he/she requests to be remembered, session and ip are respectively the session id and the current IP of the visitor.
4. Connecting to the database
function &db_connect() {
} |
This function connects to the database returning a pointer to a PEAR database object.
5. Session variables
To ease access to the current user's information we register it as session variables but to prevent error messages and set some defaults we use the following function.
function session_defaults() {
} |
to set the defaults. Of course session_start must be called before that.
6. To the core of the script
To allow easier integration with other scripts and make things more modular the core script is an object with very simple interface.
class User {
} |
This is the class definition and the constructor of the object. OK it's not perfectly modular but a date isn't much of a problem. It is invoked like:
| $date = gmdate("'Y-m-d'");
$db = db_connect(); $user = new User($db); |
Now to clear the code purpose, we check if the user is logged in. If he/she is then we check the session (remember it is a secure script), if not and a cookie named just for example mtwebLogin is checked - this is to let remembered visitors be recognized.
7. Logging in users
To allow users to login you should build a web form, after validation of the form you can check if the user credentials are right with $user->_checkLogin('username', 'password', remember). Username and password should not be constants of course, remember is a boolean flag which if set will send a cookie to the visitor to allow later automatic logins.
function _checkLogin($username, $password, $remember) {
} |
The function definition should be placed inside the User class definition as all code that follows. The function uses PEAR::DB's quote method to ensure that data that will be passed to the database is safely escaped. I've used PHP's md5 function rather than MySQL's because other databases may not have that.
The WHERE statement is optimized (the order of checks) because username is defined as UNIQUE.
No checks for a DB_Error object are needed because of the default error mode set above. If there is a match in the database $result will be an object, so set our session variables and return true (successful login). Otherwise set the failed property to true (checked to decide whether to display a login failed page or not) and do a logout of the visitor.
The logout method just executes session_defaults().
8. Setting the session
function _setSession(&$values, $remember, $init = true) {
} |
This method sets the session variables and if requested sends the cookie for a persistent login, there is also a parameter which determines if this is an initial login (via the login form/via cookies) or a subsequent session check.
9. Persistent logins
If the visitor requested a cookie will be send to allow skipping the login procedure on each visit to the site. The following two methods are used to handle this situation.
function updateCookie($cookie, $save) {
} |
10. Checking persistent login credentials
If the user has chosen to let the script remember him/her then a cookie is saved, which is checked via the following method.
function _checkRemembered($cookie) {
} |
This function should not trigger any error messages at all. To make things more secure a cookie value is saved in the cookie not the user password. This way one can request a password for areas which require even higher security.
11. Ensuring valid session data
function _checkSession() {
} |
So this is the final part, we check if the cookie saved in the session is right, the session id and the IP address of the visitor. The call to setSession is with a parameter to let it know that this is not the first login to the system and thus not update the IP and session id which would be useless anyway.

Ryan June 24, 2005 says:Hello 1. should all of the code go into 1 file? I suggest you to create a separate file for a class and a separate file for database access cridentials. Login page procedure can be a one document that shows only needed part of the HTML code. Please use require_once function for inclusion. The more files you will create, the more secure your script will be. 2. which functions should I put in the user class ? This tutorial is for a one usable class creation. All functions belong to it. 3. should I add the little tidbit of code after the class to the class? You will have to add something like ExecuteClass function in order to make all check automatically initiated. And do not forget about a class headers. 4. a download of the files as is. Please clarify this question. Waiting for your reply. Note. Sorry that I am not providing a direct code solutions. A reason is that it will not help you to learn by your self. Analyse, compile, use manual
quince June 15, 2005 says:I found you tutorial very helpful, but it leaves a few things out that I don't know the answer to. 1. should all of the code go into 1 file? 2. which functions should I put in the user class ? 3. should I add the little tidbit of code after the class to the class? 4. a download of the files as is. I'm a PHP noob, and would really like to use a secure login with both sessions and cookies. Thanks! Quince



