开发者

I want to logout on close of tab in mozilla or IE 8.0 or Chrome

开发者 https://www.devze.com 2023-03-07 00:36 出处:网络
HI, Is is possible to kill the session(Logout) if I close my tab. Please let me kno开发者_开发技巧w if you guys have implemented this.

HI,

Is is possible to kill the session(Logout) if I close my tab. Please let me kno开发者_开发技巧w if you guys have implemented this.

Thanks, Manish


From the jQuery docs (.unload() is a wrapper around the relevant browser functionality):

http://api.jquery.com/unload/

The exact handling of the unload event has varied from version to version of browsers. For example, some versions of Firefox trigger the event when a link is followed, but not when the window is closed. In practical usage, behavior should be tested on all supported browsers, and contrasted with the proprietary beforeunload event.

In other words, you need to additionally proceed the other way around for it to be reliable: give your sessions a very short timeout, and repeatedly ping your server from within each session to keep them alive.


Create a session, but don't store it with a cookie. After they close that tab, or window they will loose that session.


Use the Javascript onUnload event to call/ping your PHP logout page:

$(window).unload(function() {
  // Send an Ajax request to logout.php
});

However, Carlos Campderrós is right: you probably won't know if the user has multiple tabs open, so you risk logging them out even though all they did was close one of their tabs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号