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.
[ next page
]
|