UNIX Systems Administrator (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

Custom Session Handlers in PHP4
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.
[ Next Page ]


Comments:
Session and headers problemsdanjo11/12/08 04:02
Great scriptPatrick11/04/08 16:37
Sessions/ Working with FramesMike Foland05/14/08 12:16
No Access to PHP.ini fileKevin11/14/05 05:56
Saving a little spaceKeamos10/15/05 14:23
session passing across serversCharles Peterson08/09/05 02:14
RE: How to adjust session.gc_maxlifetime...?premlal06/23/05 07:27
Passing sessions shiva01/27/05 00:00
Performances of mysql session handlersJMattB11/19/03 08:17
passing info between serversGary Peach11/04/03 04:39
modify ONLY close methodjacopo10/11/03 12:57
RE: Are you losing part of your session?ycdtosa05/26/03 09:15
RE: Potential security threadJason L05/11/03 20:53
Are you losing part of your session?Brett Brewer05/06/03 17:14
RE: Sessions or DatabaseGregg04/27/03 20:35
Potential security thread.:TCN:.04/19/03 10:48
RE: Worked Perfectly! Thanks!Jason .04/07/03 14:33
Garbage problem solvedSimon Beer03/21/03 03:09
Worked Perfectly! Thanks!Brett Brewer03/19/03 19:46
Good tutorialRecoilUK03/04/03 22:46
It's working fine - register_globals Issue?Rhinestone_Cowboz03/03/03 17:20
RE: String session VariablesBob02/27/03 11:19
Full working version ?Laurent DINCLAUX02/04/03 00:43
Thanksvinny01/21/03 10:27
RE: Sessions or DatabaseJester01/13/03 08:57
Don't use this code!Jonathan Leppert01/09/03 10:02
RE: session_on_end how to whith no php.ini acScott Fletcher01/06/03 11:39
header problems!Truong-An10/23/02 22:53
I WISH AUTHORS WOULD TESTSteve10/23/02 12:42
Garbage Collection never runs!!?!?Voyager10710/04/02 05:25
Sessions or DatabaseUdi Landau09/09/02 11:55
RE: Garbage collection & servertime, redirectsAJB08/28/02 00:51
RE: session_on_end how to whith no php.ini acces.Mattia Wow08/19/02 06:42
Garbage collection & servertime, redirectsChris08/13/02 09:06
Look at msg 1002000 (dmiyares)!mconway08/07/02 17:08
RE: Sessionrobin brinkler07/22/02 11:41
RE: session_on_end how to whith no php.ini acces.Shefali06/21/02 02:24
Problem in Session Handling PHPSUBRAMANYAM MA06/01/02 07:38
RE: Session Handling & WebFarms (fix)Mr. I.M. Kitty05/20/02 15:59
Session Handling & WebFarms (fix)Mr. I.M. Kitty05/17/02 19:34
RE: session_on_end how to whith no php.ini acces.Janvi05/17/02 07:04
PHP3 upgrade to PHP4Mike Brockington05/16/02 06:11
RE: Passing sessions from one server to anotherAndrew Nagy05/14/02 16:40
Custom sessions and objectsTofeeq Ali05/13/02 05:57
Persistent connections to database!br0sk05/11/02 05:47
RE: How to adjust session.gc_maxlifetime...?data_banks05/06/02 21:29
Custom handler for ORACLE (varchar2)Steve Grevemeyer05/02/02 21:16
How to adjust session.gc_maxlifetime...?Loke05/01/02 04:29
frameproblemsJoerg41104/24/02 13:27
Passing sessions from one server to anotherAdam04/23/02 17:36
no php script engine exists to run the codezakreg04/18/02 03:22
RE: Timeout problemsAhmet04/06/02 08:12
can't add any code to gc partAhmet04/06/02 06:40
Sessions variables not workingbhaskar03/27/02 12:14
RE: Timeout problemsk4kch4n03/23/02 05:43
SessionDondon03/09/02 13:02
RE: Timeout problemsGreg02/19/02 05:30
RE: apache erroralex02/09/02 18:54
Session Auto_startkalyan02/09/02 15:21
RE: apache errorRadek02/08/02 17:02
RE: dbm supportsame question here02/01/02 11:18
apache erroraxismundi01/29/02 17:54
segmentation fault - timeout and a solution.dennis iversen01/26/02 10:53
Timeout problemsGreg01/24/02 06:17
A word of caution about this examplebstineman01/15/02 15:16
this example can't work in sybasefelix01/13/02 23:11
RE: read object of expired session during gcMookai01/10/02 13:13
INCORRECT SESS_READ() FUNCTIONHenry Blackman01/09/02 09:26
RE: Problem with headerMichael I. Choi01/04/02 20:12
dbm supportHans12/25/01 23:52
RE: session_start();arun11/28/01 01:45
read object of expired session during gcSven Hoffmann11/26/01 21:16
RE: Problems with register_globals turned offArielígena11/24/01 15:21
the mysql session handlingIvo Marinoff11/17/01 09:12
Alternatives to session_start(); ?Bob Bergen10/16/01 15:51
RE: Sessid changes on every pageDouglas10/16/01 03:13
Perl version of this articleCarlos Fernando Scheidecker10/08/01 15:56
Mysql does not show at data paul10/06/01 17:36
RE: session variable won't updateMichael Henderson10/05/01 11:52
RE: session_on_end how to whith no php.ini acces.G|ga10/05/01 06:00
RE: Garbage Collect - How To ???G|ga10/05/01 05:44
RE: Session problems in PHP4 under IIS 5.0 WIBjorn10/04/01 21:29
RE: ClassesG|ga10/04/01 09:52
Classesmm2410/03/01 06:01
Garbage Collect - How To ???Ziggi10/01/01 04:55
session variable won't updatehshek09/29/01 19:56
RE: Session problems in PHP4 under IIS 5.0 WIN2KSteve LeVine09/20/01 06:27
RE: Problem with SID using header()Jason Dunbar09/05/01 01:31
RE: sessions with framesCFP08/30/01 04:21
User online: a question. Zine08/27/01 00:21
NetscapeMikail08/13/01 14:53
Session - connection_aborted()whoelse08/08/01 06:38
(session_onEnd) session_destroy() ilker07/25/01 01:01
Session problems in PHP4 under IIS 5.0 WIN2KArbitr07/24/01 02:25
RE: session_start();Mike07/23/01 06:19
Numbers returnedyaqi07/19/01 20:20
sess_gc()Chris07/02/01 12:35
RE: session countDavid Altherr06/30/01 19:12
session_on_end how to whith no php.ini acces.dunsun06/25/01 02:15
RE: String session Variablesjas06/03/01 12:15
String session VariablesKavitha P05/15/01 23:09
MySQL & CookiesPiLoT04/25/01 10:44
upload password with then session passwordjakry04/24/01 08:17
Magic quotes corrupting my session values?Simanta04/19/01 02:22
Frustration -- solved?Dave Hathaway04/18/01 15:35
Problems with register_globals turned offApu04/18/01 05:00
RE: PERL o PHPraptor04/10/01 09:44
RE: session counttajson04/01/01 16:37
PHP ini to allowMonte Shaffer03/31/01 15:57
Session never destroyedIan03/28/01 13:02
problem wrt session.gc_maxlifetimeEric Blade03/26/01 01:30
RE: How do I find out whether session is expired?Jani Kaarela03/12/01 09:11
PERL o PHPClaudio Martinez03/07/01 16:18
better sess_write in mysqlBastian Hoyer03/07/01 12:44
Notes on DB Session handlers and Objects.The Tweaker03/02/01 13:12
sessions with framesChris Hayes02/01/01 17:00
RE: i loose my sessionCybernd01/25/01 15:21
i loose my sessionangus01/18/01 09:46
Mysql version has some kind of errorCybernd01/06/01 10:21
RE: PHP as a module and php.iniTroels Arvin12/30/00 16:07
RE: session_start();Idle12/19/00 01:30
Where to put itDebersaques Bart12/08/00 04:42
DBM sessions help!rtm12/07/00 07:40
I cannot test session handle using dbm,help! robbinwoo11/25/00 20:40
Umm... but what about PHP.INI?dmiyares11/23/00 12:19
Really having problem with SID using header()Marcello Junior10/31/00 00:49
RE: Problem with SID using header()David Garcia10/27/00 04:25
Problem with SID using header()David Garcia10/23/00 12:46
session countalf10/20/00 03:36
Bad implementation in library (or in PHP?)?Petr Spevacek10/16/00 03:11
Storing objects or arrays in the session...?Joe Asdf09/09/00 01:19
Problem with headerJulien Bailly09/07/00 05:22
Problem with headerJulien Bailly09/07/00 05:14
RE: Warning:Handler can lose the database -MySQLDamien08/31/00 20:12
Other Custom Session HandlerAlejandro08/26/00 18:17
Performance of MySQL Session HandlerNathan Cassano08/25/00 15:16
How do I find out whether session is expired?Lubes Haidamaka08/23/00 02:15
imap stops working...Andrei Ivanov08/15/00 14:48
safe_mode and session_dbm.phpDominik Guder08/08/00 20:19
href problem for transparent SID passingrob08/08/00 08:03
RE: Sessid changes on every pageAndreas Zekl07/31/00 09:51
RE: EXIT()Federico07/31/00 05:17
RE: Sessid changes on every pageAndreas Zekl07/27/00 10:16
Sessid changes on every pageBealers07/27/00 07:18
RE: PHP as a module and php.iniFederico07/19/00 10:45
RE: Not working with PHP 4.01pl2Chris Moylan07/18/00 13:34
RE: Not working with PHP 4.01pl2Hutch Hicken07/18/00 13:09
RE: Not working with PHP 4.01pl2Hutch Hicken07/18/00 12:05
RE: Warning:Handler can lose the database -MySQLFederico07/17/00 22:22
RE: Not working with PHP 4.01pl2Dan Stefura07/14/00 14:13
RE: Not working with PHP 4.01pl2Ying Zhang07/13/00 11:23
RE: Warning:Handler can lose the database -MySQLYing Zhang07/13/00 11:21
problem with back button?seb sol07/13/00 06:47
RE: Not working with PHP 4.01pl2Dan Stefura07/10/00 23:36
Not working with PHP 4.01pl2Dan Stefura07/10/00 23:24
RE: sess_write - with PHP 4.01pl2Dan Stefura07/10/00 21:41
sess_writeCarlos Green07/10/00 15:11
Warning:Handler can lose the database -MySQLFederico07/09/00 07:47
RE: Missing function call in MySQL sess_read() Ying Zhang07/07/00 13:43
Missing function call in MySQL sess_read() David Osborne07/07/00 07:03
session_start();Mohd Hanafiah06/29/00 22:09
session.save_path and shared memoryHroi Sigurdsson06/29/00 17:51
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.