views:

38

answers:

2

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.

A: 

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

Jon Smock
A: 

Check the following link for best answer: http://stackoverflow.com/questions/805895/how-come-closing-a-tab-doesnt-close-a-session-cookie .

What you can do is append some random text in end of url's and store it for reference and if the users comes from new tab and doesn't has the random text appended, you can determine the user coming from different tab.

Aman Kumar Jain