Clearing session values ​​when closing a tab

As far as I understand, the session variable is cleared when users close their browser. Is there a way to clear a session variable when users close a tab in a browser?

I ask that I need to distinguish between two visits if the user is on the site and closes the tab, but not the browser, and the user returns to the site on a separate tab in the same browser session.

+6
browser ruby-on-rails session-variables
source share
2 answers

Session variables are server-side, and closing tabs is a client-side action, so you need to somehow send a signal to the server to clear these session variables.

The most obvious way for me would be to use the onbeforeunload browser method and ajaxically send something to the server to clear the session.

This is a dangerous territory. Are you sure you do not want to allow the user to open and use several tabs of your site at the same time? Because if I have your site open on two tabs, this method will clear the session when one tab is closed, making the other tab useless (not useless, only the rug may have been pulled out from this page, now this session is missing).

+2
source share

For the best answer, check the following link: Why does the Delete Session Cookie tab not close? .

What you can do is add some random text to the end of the URL and save it for reference, and if users come from a new tab and don't have random text added, you can define a user coming from different tabs.

0
source share

All Articles