The PHP script is as follows:
I don't think this is how gc_maxlifetime
is supposed to work. The manual says
session.gc_maxlifetime specifies the number of seconds after which data will be seen as 'garbage' and potentially cleaned up.
(emphasis mine)
in your case, the session is still active. Therefore, I don't think it will be subject to garbage collection.
You could try doing a session_write_close()
before the sleep(). That might increase the probability that the garbage collector.
session.gc_maxlifetime is the number of seconds after which the session will be considered for garbage collection.
session.gc_probability and session.gc_divisor then determine the probability that garbage collection will be executed on any session initialization
Read the manual (emphasis mine):
session.gc_maxlifetime
specifies the number of seconds after which data will be seen as 'garbage' and potentially cleaned up. Garbage collection may occur during session start (depending onsession.gc_probability
andsession.gc_divisor
).
In the same page:
session.gc_divisor
coupled withsession.gc_probability
defines the probability that the gc (garbage collection) process is started on every session initialization. The probability is calculated by usinggc_probability/gc_divisor
, e.g. 1/100 means there is a 1% chance that the GC process starts on each request.session.gc_divisor
defaults to 100.
Now do the math and see that it's not very likely the GC will be called on each request.
You should store the in the session a variable that saves the time of the last activity of the user and use that instead of the session is logically "active". Don't rely on garbage collection.
Even if the garbage collector kicked in and deleted the session file you opened/read with session_start()
, it will NOT reach into the guts of that particular PHP process and delete the $_SESSION
object array.
Assuming you're on the standard file-based session handler (which contains a serialize()
'd copy of $_SESSION
), here's what happens.
session_start()
, causing PHP to open/lock the file, read its contents, deserialize the data, and incidentally, possibly update the session file's "last used" timestamp (atime on Unix boxes).Now, if you did something like this:
ini_set(...); // set GC probability to max, short session lifetime, etc...
session_start(); // populate $_SESSION
session_write_close(); // dump $_SESSION out to file, close file, release lock.
sleep(7); // Sleep for 7 seconds;
session_start(); // re-populate $_SESSION;
Now you might just end up with a fresh blank $_SESSION, IF the garbage collector decides to kick in. However, unless you do that second session_start()
, the old $_SESSION data from the previous start() call WILL STILL BE PRESENT. The session file may have been trashed, but the garbage collector will not touch what's present in your script's memory as it runs.