[thelist] PHP sessions timing out

Juha Suni juha.suni at ilmiantajat.fi
Mon May 2 08:52:18 CDT 2005


charles stuart wrote:
> We came upon this solution:
> http://us4.php.net/manual/en/function.session-cache-expire.php
>

I would like to point out that you should really read a bit about PHP's 
builtin garbage collection, which clears up sessions every once in a while 
(its actually random). If you are on shared hosting, or if you have a lot of 
different scripts or pages dealing with sessions, it is quite propable that 
other scripts than the one you are tweaking are using the same tmp-directory 
to store their sessions too. When the garbage collection kicks in for those 
scripts, you sessions will go along with the collector too, no matter what 
session timeout you have specified for those sessions in your script.

This happens since the garbage collection started by those other scripts has 
no idea how long it should be storing your scripts' session files if they 
are in the same directory. Actually it doesn't even care about who's session 
is what. All it looks at is the last modified timestamp of the session 
files, and if they are older than gc_maxlifetime (which defaults to 1440 
seconds, being 24 minutes, on those scripts that have not stated otherwise) 
it deletes them happily away.

A rather easy and solid solution should be to set your script to store it's 
sessions elsewhere, where others scripts's garbage collection is not messing 
around. Another way is to globally increase the gc_maxlifetime from php.ini, 
but that might not be an option on shared hosting.

I am a bit surprised how little of info about garbage collection is usually 
given when dealing with sessions-documentation. Since it is a random little 
bugger it can easily cause hard to track weird session timeouts if its 
presence is not taken into account. Especially on shared hosting, or servers 
with multiple sites running.

--
Terveisin, Best Regards
Juha Suni

mainostoimisto
I L M I A N T A J A T
Kirkkokatu 31
90100 Oulu
tel. +358 - (0)8 - 377 580
fax. +358 - (0)8 - 377 560
mob. +358 - (0)40 - 766 4400
http://www.ilmiantajat.fi



More information about the thelist mailing list