Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Custom Session Handlers in PHP4
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:
  1. sess_open($sess_path, $session_name);
    This function is called by the session handler to initialize things. The two parameters passed to it are $sess_path, which corresponds to the session.save_path setting in your php.ini file, and $session_name which corresponds to the session.name setting in your php.ini file. More on the duties of this function when we go into specific examples.

  2. sess_close();
    This function is called when to when the page is finished executing and the session handler needs to close things off. (Note, do not confuse this with sess_destroy(), which is called to kill the session).

  3. sess_read($key);
    This function is called by the session handler to read the data associated with a given session key ($key). This function must retrieve and return the session data for the session identified by $key. (Note: you do not have to worry about serializing and unserializing data, if you do not know what this means, then don't worry about it).

  4. sess_write($key, $val);
    This function is called when the session handler has session data to save, which usually happens at the end of your script. It is responsible for saving the session data in such a way that it can be retrieved later on by sess_read($key).

  5. sess_destroy($key);
    This function is called when a session is destroyed. It is responsible for deleting the session and cleaning things up.

  6. sess_gc($maxlifetime);
    This function is responsible for garbage collection. In the case of session handling, it is responsible for deleting old, stale sessions that are hanging around. The session handler will call this every now and then.

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).
[ Next Page ]

[Page 1]  [Page 2]  


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.