简体   繁体   中英

How to kill session on browser tab or browser close in js?

I am trying to destroy session on tab or browser close. I tried this beforeunload . but this close session after page refresh. I don't want to destroy the session after page refresh. How can i do this ?

The nature of http is stateless, so there's no way to differentiate between closing tab and page refresh.

If what you are trying to achieve is to prevent cases where users forgot to logout (destroying the session themselves) after using your web, what you could do is to have a short-lived session (10 minutes for instance) and reset the the session life back to 10 minutes every time the user does something. If the user is inactive for 10 minutes, the session would expires by itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM