To my understanding the session variable gets cleared when a users closes their browser. Is there anyway to clear the session variable when a users closes a tab in a browser?
The reason I ask is that I need to differentiate two visits if a user is on the site and closes the tab but not the browser, and the user goes back to the site on a separate tab in the same browser session.
Closing tabs does not end sessions only until the browser is closed will it destroy the session.
Browsers deletes the session cookies when the browser is closed, if you close it normally and not only kills the process, so the session is permanently lost on the client side when the browser is closed.
We can clear the session storage by using the clear() method.
Session variables are server-side, and tab closing is a client-side action, so you'd have to somehow send a signal to the server to clear those session variables.
The most obvious method to me would be to use the browser's onbeforeunload method and ajaxically send something to the server to clear the session.
This is dangerous territory, though. Are you sure you don't want to allow the user to open and use more than one tab of your site at the same time? Because, if I have your site open in two tabs, this technique will clear the session on the close of the one tab, rendering the other tab useless (not usesless, just the rug might have been pulled from under this page, now that session is gone).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With