Clear user session variable when browser is closed

Clear user session variable when browser is closed

  

 Hi All,

I have a scenario where I am creating session variable to hold value throughout the session, but when I close the browser I want to clear the session variable. Can anyone suggest me how to achieve this?

Thanks in Advance

Hello Sneha,

It seems to me that you are using a Session variable in a way it is not supposed to be used.
The idea is that a session variable is used where you want to keep the value that is there while the session is alive.

Closing the session will reset the session variables.But close the browser does not close the session.

So, if you are using a session variable to store temporary data, you can think of using local variables instead, or reseting the value when the process starts, ignoring any value previously stored in it.

Cheers.

Hi Sneha,

Actually... - and I'm sorry to contradict you Eduardo ;) - as far as I'm aware, closing the browser will close any session established between that browser and a web server (and that means any values the session variables held would be discarded)... but we are talking about closing the browser application, not just a browser tab or one of the browser's windows.

Have you tried to see if this isn't already working out of the box Sneha?...

Hi Jorge,

I would say you are right. The session id is in a cookie and by default, unless the cookie has an expiring date, it will be killed if you close the browser. At the moment I can't test.

But I still think this requirement shows an use of the session variables that probably is not being done as it is supposed.

Cheers.

Hi,

The client's session cookie will be removed when the browser closes, but the session will live on on the server for the remaining time of the session timeout, which is default 20 minutes. 

Since the client cookie doesn't exist anymore, there's no way to access the session information on the server. If you really want to clear the session info, maybe you can use the OnLoad of OnBeforeOnLoad events to trigger a logoff action?

Hi Lennart and Jorge,

I'm sorry to say, but both of you are partially wrong :P

I tested in all browsers I have in my computer and the behaviour was not the same after close and open the browser again, regarding the session variable values:

  • Chrome: Keep 
  • Edge: Do not keep
  • Firefox: Do not keep
  • Opera: Keep
  • Safari: Do not keep

So, this is not a standard behaviour (kill the cookies), and you can't rely on this to guarantee that the value of a session variable will be reset...

But I will still keep saying that if you need this behaviour, probably you shouldn't be using a session variable...

Cheers.

P.S. My first answer was because of previous experiences with Chrome, my main browser, but as I do not looked into this in quite some time, and in theory brwoser do not keep cookies, I thought I was wrong, Jorge. Seems I was not. xD

@Eduardo,

You partially are... and you partially aren't. Chrome (and possibly others) have settings that prevent session cookies from being cleared for simple usability reasons (you can check this post for more information about it). Depending on what's Sneha's goal, that might be acceptable or not.

@Sneha,

Can you further detail why you have such requirement? What are you trying to achieve?

Hi @Jorge :)

Yes, it is configurable. 

And that's the reason why I think you can rely on this feature. You simply can't rely on a behaviour that is configurable, as if it wasn't. Just because of the user, that can configure the browser in a different way than what you may need.

If Naha needs to guarantee that the value is reset if the browser is closed, it will not be easy. I've being reading on this issue and there are some alternatives, most of them do not works on all browsers, all of them have caveats. 

So, yes, it's important to understand better the requirement.
But I would say that probably a change in the way the requirement is being done, avoiding use Session variable, probably will provide a better way to deal with the issue.

Cheers