0

I'd like to induce a function after the session is destroyed (i.e. user closes the browser). This function would change a value in my database from true to false. Is that possible?

Ziarno
  • 7,366
  • 5
  • 34
  • 40
  • 6
    It is impossible for the server to know when the user closes the browser. – SLaks Feb 04 '13 at 15:47
  • Session data is destroyed when garbage collection runs and session file exceeds max lifetime. It's never related to browser events. – Álvaro González Feb 04 '13 at 15:49
  • 2
    You could try firing a request on an onunload event, but there is no guarantee that it would work (it will also get fired on navigation to another page). If you are tracking sessions in a database, the normal way is to assume it will expire after a certain point of time. – datasage Feb 04 '13 at 15:49
  • If you could be a bit more descriptive about what your end-goal is here we may be able to suggest a better way to go about it. – Sammitch Feb 04 '13 at 15:53
  • Tell us the reason and it will be easier to find the best way toa rchive the result. as you asked it's just plain impossible. – Danilo Kobold Feb 04 '13 at 16:07
  • In admin panel I have a button that opens and closes an online store - when it's closed users cannot buy items on the website (the website checks the database if the status is either true or false). Basically the shop can be open only if there is an admin online in the admin panel. I'd like to close the shop (set the status in database to false), if the admin forgets to click the button "close shop" and just closes the browser window (because users shouldn't be able to buy items without the admin present). – Ziarno Feb 04 '13 at 16:20

3 Answers3

0

I'm guessing that you're wanting to figure out whether a user is online or not (or something similar?) The only way to "know" (read: guess) that a session is dead is to store the time that the user was last active and then compare that time against the current time. Then, depending on your needs, you can decide to make the assumption that a user who has been inactive for 30 minutes is offline.

Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
0

Only way to do this is using Javascript to detect a close and using a Ajax request (using XMLHttpRequest for example) to your PHP page (which handles the logout part).

See Browser/tab close detection using javascript (or any other language) for more information on the closing part.

Community
  • 1
  • 1
Devator
  • 3,686
  • 4
  • 33
  • 52
  • I think this would work, however the onunload event occurs also when user clicks on a link or refreshes the page - I'd only like and event that occurs when user closes the browser's window or tab. – Ziarno Feb 04 '13 at 16:37
  • @Ziarno Some websites have the `Are you sure you want to leave?` question. Check this out: http://stackoverflow.com/questions/1119289/how-to-show-the-are-you-sure-you-want-to-navigate-away-from-this-page-when-ch - this is the way you should use. – Devator Feb 04 '13 at 16:41
  • I don't want the function to fire on refresh though. I guess there isn't a way to just have an event that fires on tab/window closure and not on refresh/link... – Ziarno Feb 04 '13 at 17:02
0

I would suggest you to register the user last activity in your database, this way it's 100% accurate. Then by comparing the registered date with the current time, you can decide when the user is active or not.

If you really want to know if the user is still on your page, you could also do something, but you should avoid it : On the client side, make a javascript loop, for example each minute, that tell to your server the client is still there. When the client will close his page, the loop won't make any new query.

yohannc
  • 140
  • 2
  • 9