Reputation: 124
I don't know if this is possible. What I'm looking for is the event (if there is one) that launch's to end the session. I'd like to somehow grab that and perform my own action right before the session is ending. The reason for this is I have need for one of the session variables set that I will lose as soon as the session expires.
I feel this is probably a poor way of achieving my goal if even possible. It's friday 7:00pm and still at office. imagine my excitement trying to find a solution.
Thanks Guys.
Upvotes: 2
Views: 2472
Reputation: 4320
Technically it's not possible to tell when session that is stored by PHP (session as in server-side cookie) will be killed. In PHP settings you can say how long a cookie must survive, but it can last longer than that set time.
If you need to keep track of how long it lasts exactly, you should build a session management system manually and not use the PHP one.
Upvotes: 0
Reputation: 98559
You can sort-of do this with Javascript. Have the server deliver a JS function that will sleep until the session is about to expire.
However, this isn't foolproof: when the user leaves your page, tough cookies, they're not running your JS anymore. Of course, you also can't get in touch to tell them whatever you'd say when the session expires...
You could also have your sessions "expire" before you really remove the data, and have a custom handler that alerts the users and gives them a grace period. The second time the destroy handler triggers, actually remove the data.
Upvotes: 0
Reputation: 11623
You could save the data in a cookie and control the expiration date:
setcookie("TestCookie", $value, time()+3600); /* expire in 1 hour */
http://php.net/manual/en/function.setcookie.php
This way you don't need to change the regular session functionality and allow them to expire properly.
Upvotes: 0
Reputation: 69977
If you make your own session save handler to use a database for example, you can have a callback for garbage collection that you could use to grab the appropriate data prior to deleting the data.
See session_set_save_handler, in particular, the gc
callback.
Upvotes: 2
Reputation: 18843
If you have database access you can store the session info in a session table and retrieve that variable from the last session registered by that specific user.
That I know of, there is no solid way to trigger an event just before session termination that is guaranteed to work. It's in your best interest to restructure how the data is being handled rather than come up with a brittle hack.
Upvotes: 1