Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing session values on tab close

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.

like image 938
scott Avatar asked Aug 20 '10 15:08

scott


People also ask

Does session end when tab is closed?

Closing tabs does not end sessions only until the browser is closed will it destroy the session.

Is session destroyed when browser is closed?

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.

How do I remove session variables in browser?

We can clear the session storage by using the clear() method.


1 Answers

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).

like image 185
Jon Smock Avatar answered Oct 13 '22 12:10

Jon Smock