#native_company# #native_desc#
#native_cta#

Custom Session Handlers in PHP4 Page 4

By Ying Zhang
on July 30, 2000

A MySQL Session Handler

Our next example is to write a customized session handler to save session
data into a MySQL database. (This is the session_mysql.php file from
ying20000602.zip.) You would want sessions stored in a database when you have
lots of web/PHP servers and you need to share session between them (eg. if you
are serving so many users that you need load balancing). You have a bunch of
machines doing web/PHP stuff, a machine serving your normal database needs, and
another machine running a MySQL database to handle sessions. But that might be
overkill for most people 🙂

IMPORTANT NOTE:

You must have MySQL support in your PHP4 before you try this.
If you do not things can get ugly, real ugly!

First let’s create a session database in MySQL, and then the table to
create the session table. Fire up your MySQL client and issue these
commands:
mysql> CREATE DATABASE sessions;

mysql> GRANT select, insert, update, delete ON sessions.* TO phpsession@localhost
    -> IDENTIFIED BY 'phpsession';

mysql> CREATE TABLE sessions (
    ->     sesskey char(32) not null,
    ->     expiry int(11) unsigned not null,
    ->     value text not null,
    ->     PRIMARY KEY (sesskey)
    -> );
	
Next, modify the $SESS_DB* variables in the session_mysql.php file to
match your database setup. Make sure it all looks okay before you continue.