2

I have a nice LogOut button on the menu of my PHP app, but some people don't use it, they just quit the browser.

Is there any way that I can detect quiting the browser and/or closing the last tab with my App open? (I realize that it won't handle browser crash, but it's better than I have now & covers 99% of cases).

I undestand that I can't use JavaScript since the DOM won't let my JS be aware of other brower tabs and windows.

I used a timer based solution, but a single user set the timeout to 999999 minutes and the number of simultaneous users to 1, then closed his browser & locked himself out.

But, what about another way? Think laterally, folks. Can I push a cookie when the user opens a new tab with a page from my app? And then remove the cookie when the user wishes to close the tab? And check if I am remvoing the last cookie & inform the server to treat this as logout?

If not cookies is there some other method? (Use Ajax on page open & close and the server can decide if the last tab has closed?)

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • 2
    You could set an aggressive [session timeout](http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes) maybe at, say, 60 seconds, and use a ping AJAX call to update the time that ran every, say, 20 seconds. Or use three minutes/one minute, or some other combo. – Jared Farrish Oct 18 '11 at 02:25
  • 1
    on every page Ajax hits php script every 10 seconds with user credentials, 20 seconds no hit = they have left. way to much overhead for my liking –  Oct 18 '11 at 02:27
  • +1 each. that might work, if I rein it back to a minute or so. It's just to stop user licences from being tied up and is a definite edge case, but I am interetsed in the concept genrally. – Mawg says reinstate Monica Oct 18 '11 at 02:37
  • Jared++; That's how I'd do it. – Syntax Error Oct 18 '11 at 02:51

1 Answers1

2

How about catching the unload event of the web page, so that when it unloads, it uses ajax to call a script on the server. This script will then store the time that the browser calls it.

If within 20 seconds, the browser accesses another page that uses the same session, this will mean that the user is just navigating around your site. then you can safely unset that time stored from the ajax call.

At the server end, have a cron job that runs every minute (5 if you are worried about resources) that checks if any of the session has the unload time set, and that the unload time is longer than 25 seconds. If so, then it means the user has closed the browser, tab, or has navigated away from your app. It should be save to do any clean up or logging out for that browser then.

You might need to adjust the timing depending on how the internet/server speed and how fast the page refreshes when the users navigate around.

Brandon
  • 16,382
  • 12
  • 55
  • 88
iWantSimpleLife
  • 1,944
  • 14
  • 22