tags:

views:

47

answers:

5

I would like a function to run whenever a session is destroyed. I've looked into using session_set_save_handler but I don't want to manually handle the storage of sessions, I just want to do a little processing when they're destroyed. Is there any way to do this without using session_set_save_handler?

+1  A: 

The handler is pretty much the only method I think. A session can also be destroyed as part of the garbage collection and both of those can only be intercepted by using the session_set_save_handler that I know of.

Blizz
A: 

You can do it with register_shutdown_function(). The function is executed when the script exits.

Edit: Since my answer above was seen to be incorrect, I'm editing to give a better answer. You can call whatever function you like before or after you call session_destroy(). Session is erased only when you call session_destroy() explicitly. Note that PHP also cleans expired sessions in a cron job, if one is set up by the system administrator, but you have no way of affecting that cleanup from a php script.

jmz
And what would be the use of having a session if it is cleaned up at the end of the script? :)
Blizz
Made the same mistake in reading it the first time. Session destroyed or session closed are indeed different things.
Wrikken
Seems like we did make a mistake. I've revised my answer. Hope it's helping.
jmz
The cleaning of the expired sessions is called garbage collection. You can also be notified of this by setting a gc callback function using session_set_save_handler.
Blizz
A: 

The only way as I see it is to turn of garbage handling, and do that yourself. Wouldn't work for memcached based sessions, but file-sessions are easily traceable / loadable / removable (just look for files not recently accessed in the session directory).

Wrikken
A: 

If you're not managing sessions manually, then they're most likely stored as files, and like Wrikken said, you could delete older session files yourself with a cron job.

But really, that's the job of the garbage collector. And the whole session_set_save_handler isn't that hard to use. You can install a ready-made, and just trigger your function when the garbage collector is invoked.

Mikael Gramont
A: 

This is something that's easily done in other languages that have a full application platform like Java, .NET, etc. For example you have events that are fired by the server at startup, application startup, session end, etc. With PHP there is no real concept of an "application" in the same sense as the other languages, so you don't have those events that are fired by the app server. But there are things you can do manually.

Probably the easiest thing to do is to simply timestamp a DB record for each user that is logged in on every page load. Then you have a cron job running to look for timestamps that have expired (meaning the user hasn't loaded another page within 20 minutes - or whatever your timeout is). You can then do whatever action is necessary. a nice bonus of this is that you can write something to monitor which users are logged into your system at any given moment.

Another idea is that you could actually write something on the server to look through the PHP folder that contains the session files. However matching these up with users would probably be difficult.

Jason