2

Can I send a GET request to a logout URL when a user closes a browser tab so that his session is destroyed?

EDIT:
This is actually a keepalive page. Users should keep this page open. It refreshes at TTL to keep the session active. However some users close the keepalive page and resume working. And yeah,, it's not scalable. So I have a TTL of 20min. I can't decrease the TTL as it increases load on server. If they close the page, I want them to be logged out.

Binoj D
  • 115
  • 2
  • 13
  • Nope. Even AJAX requests are not guaranteed to succeed on close. – Joseph Feb 22 '13 at 02:54
  • Sure you can, you just have to send the request when `onbeforeunload` fires, but there's no guarantee that there will be enough time to send the request before the browser closes. From experience it works in some browsers, but not all. – adeneo Feb 22 '13 at 02:54
  • Possible in a VERY ugly way: open a window at `onbeforeunload` event and destroy the session in that window. – Passerby Feb 22 '13 at 02:54
  • "Are you sure you want to close this window?" - "yeah, of course" - "BTW, thanks for letting me send that AJAX". – John Dvorak Feb 22 '13 at 02:55
  • 3
    Why do you want to log them out when they close the tab? What if they have your website open in multiple tabs? What if they want to come back without having to log in again? – Explosion Pills Feb 22 '13 at 02:56
  • you can use `setInterval` send 'heartbeat' signal to server. – anhulife Feb 22 '13 at 02:57
  • This is actually a keepalive page. Users should keep this page open. It refreshes at TTL to keep the session active. However some users close the keepalive page and resume working. And yeah,, it's not scalable. So I have a TTL of 20min. I can't decrease the TTL as it increases load on server. If they close the page, I want them to be logged out. – Binoj D Feb 25 '13 at 05:39

1 Answers1

0

I think this post contains the information you need.

You need to modify it a little though, so it does what you need (sending a logout request).

Community
  • 1
  • 1
andri
  • 1,021
  • 1
  • 9
  • 16