i need to extend the garbage collector of php so when it deletes an old session it should also delete some database entries.I have tried using implementation of SessionHandlerInterface but it doesnt work. Where should i add my code? Is it possible or these implementations have to do with standard session_destroy calls and not the garbage collector destroy?
2 Answers
The function session_set_save_handler allows you to attach callback on many session events (open, close, read, write, destroy and gc). In your case, destroy
and/or gc
might do it.

- 86
- 2
-
I have tried both methods, it seems it doesnt being called. Can you provide an example? – user3615971 May 09 '14 at 11:31
-
Here is a good exemple implementing the `gc` method : [here](http://php.net/manual/en/function.session-set-save-handler.php#60316) – WickedYeti May 09 '14 at 11:41
-
I don't need to change the whole handler process, just to do an extra step when garbage collector deletes a session. I want to keep the current functionality of saving into a folder. – user3615971 May 09 '14 at 12:06
-
check my code, the problem is that no session data are being written, the session is crated but i cannot pass anything in $data of write method. Normally when using $_SESSION['user']='user' it should triger the write method having 'user' in $data variable. – user3615971 May 09 '14 at 17:15
According to the documentation :
Destroys a session. Called by session_regenerate_id() (with $destroy = TRUE), session_destroy() and when session_decode() fails.
So, a destroy function defined with SessionHandlerInterface
will only be called when the session is explicitly destroyed during a script, or in case of error.
To destroy the old sessions, you have to use the gc
argument of session_set_save_handler
and remove all entries with datetime < currenttime - $lifetime
.
EDIT :
Note that there is only a probability of 1% of having gc
called at the beggining of a query. It means that it's execution highly depends on how many traffic you have on your server. When you're alone working on a test server, you can't easily have it executed.

- 4,649
- 1
- 21
- 32