views:

63

answers:

2
+2  Q: 

PHP Session help

Hello,

I have a session that works perfectly expect for one, if I close the browser the session gets destroyed however if I close the current tab and then go back to the site, the session still exists, how can I make sure that the session is destroyed both on a tab close and a window close?

A: 

You can't check tab closing with php, you should do it with a combination of the javascript onunload event and ajax call to request the destroy method for the server side session.

Anonymous
+2  A: 

The problem here is browser behaviour. Cookies aren't usually destroyed until the browser is closed, and PHP sessions are maintained via a session ID cookie.

Your best bet may be to set the session timeout to something shorter than the default (15 or 30 minutes I believe)

You could try and do something with onunload as Anonymous suggests, but the onunload event is not guaranteed to fire so you won't be certain that the session has been destroyed.

Is there a particular reason you need the session to be destroyed straight away? If we know your exact problem we may be able to suggest a workaround

Neil Aitken
You need to be more precise: “Session cookies aren’t usually destroyed until …” But don’t confuse *session cookie* (cookie that is only valid within one browser session) and *cookie for session ID* (cookie that stores an application’s session ID).
Gumbo
Agreed, in this case I am referring to the cookie containing the session ID. Typically PHPSESSID for a default PHP install.And of course depending on the browser setup, there isn't actually a guarantee that this will be destroyed on browser close either.
Neil Aitken