![]() Join Up! 96813 members and counting! |
|
|||
Custom Session Handlers in PHP4
Ying Zhang
This document describes how to customize the session handlers in PHP4. We will
provide examples of how to write a fully functional session handler that works
with DBM files and one that works with a MySQL database.
New to PHP4 was a set of native session handling functions which was badly missing from PHP3. By default, each session is stored as a separate file in your temporary directory (eg. /tmp in Unix). This may or may not be appropriate depending on your requirements. For example, if you have a bunch of web/PHP servers on different machines, you can't easily share sessions between them (well you could save the sessions on an NFS share but that would be slow). Another problem is that you potentially have thousands or even millions of session files cluttering up your file system (you run a big site right <grin>).
Fortunately for us, the developers of PHP4 were forward thinking enough
(thanks!) to provide the ability for users like you and me to extend the
session handling routines.
This document is written to explain these session handlers a bit and to
provide two working examples of how you can extend the session handlers.
Our first example will be to make the session handlers save session data
into DBM files. Our second example will have our sessions save into a
MySQL database.
Before you proceed, download the file
ying20000602.zip and extract it into your
web document directory.
Session Handler Functions
Any custom session handler we write will have to provide 6 basic functions,
they get called by the PHP4 session handler so you do not need to worry about
calling them yourself. The nice thing about all this is that your custom
session handler functions are completely transparent, so you can change them
without affec
The functions are are:
So now we know what functions we have to provide, they don't necessarily
have to be given those names but they have to accept those parameters
(whether you need them or not).
A DBM Session Handler
Our first example is to write a customized session handler to save session
data into a DBM file. (This is the session_dbm.php file from ying20000602.zip.)
There are many reasons why you might want to do this,
for example, if you were on a shared server from your ISP and you did not
want your sessions mixing with those from their other client's scripts.
IMPORTANT NOTE:
The approach we are going to take is to have one DBM file that stores
all the session data (in case you don't know, a DBM file is like a very
simple database that only holds key/value pairs). To fit this into the
6 functions:
Why? So that incase it has expired, but hasn't been removed out for
whatever reason, we don't accidentally read in expired data. This
would be a big no-no!
We know that DBM only stores key/value pairs, so we have to glue
the timestamp onto the value when writing the session data, and extract
it when reading in the session data. Any session who's timestamp has
been expired will be ignored. See the source code, it will make more
sense there.
So now we've got a DBM session handler, cool! Now let's get these
sessions stored in a MySQL database.
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:
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.
Our 6 functions will now work against a MySQL database:
Now we've got a MySQL session handler, pretty easy wasn't it?
Conclusion
This concludes this little tutorial, hopefully you have a good feel for how to
extend the session handling functions in PHP4. The examples here are just simple
ones to demonstrate how you would do it, extend them to accomodate your needs and
if you find any bugs, please let me know :)
|