Kill Session On Page Exit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Kill Session On Page Exit

Post by donny »

Hello,

is it possible to kill a session when the user exits a certain page?

i only have a few pages on my site that use sessions. i want the session to be destroyed when they leave one of the pages. weather it be they x out, go to a page that doesn't have sessions, or go to a different website.

thank you.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Kill Session On Page Exit

Post by Celauran »

If they go to a page where you don't want to use sessions, you can destroy the session right there. For pages you don't control, your best bet would be to set a timeout value on the session.
nadya.fd
Forum Newbie
Posts: 4
Joined: Mon Sep 08, 2014 11:57 am

Re: Kill Session On Page Exit

Post by nadya.fd »

You also could try to navigate user to the new page ( = leave current page) from this current page (use header()). -> so at the point you redirect user you can also do anything else (for example kill session).

This is may not be a good idea, but what if... just an option.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Kill Session On Page Exit

Post by donny »

basically i am making a order status page and they login by typing their email and order number.. it allows them in if there is a row that exists with that info.. it stores that info in a session. so the order status page will let them in without having to re-login again.. but i want this session to expire after sometime i don't want it to last until it kills itself overtime however it does that. how can i make one that is based on time?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Kill Session On Page Exit

Post by Celauran »

I think a timeout or expiry is the best option here since you can't reliably catch events like them closing the browser or navigating to another site. When you create the session, set an expiry value X time in the future. On any subsequent request, check the current time against that stored value. If the expiry time has passed, kill the session and make them login again.
Post Reply