views:

471

answers:

2

I have a method I need to run on Session_end. Is there any way I can ensure the code runs when a user closes his browser? This may be a dup queston. I saw one response doing a call to ajax unload or something, but I don't want this to fire everytime a user navigates away from a page, just when they close their browser.

Thanks, ~ck

+1  A: 

Simple answer: You can't control user's browser. The AJAX solution you mentioned is the closest you can achieve (without a plugin/custom client.) Imagine some Web site could track every time you opened and closed your browser window. Don't you think it would be a privacy issue?

The truth lies in the stateless nature of HTTP. The Web server is out of the picture as soon as it finishes sending the response to the client. After that, all you can rely on is a client side script (which only executes because the client wants it to; and it can easily choose not to run scripts.)

Mehrdad Afshari
A: 

Session_end is unreliable. No matter what you do.

Session_end only fires when you are using inprocess session. If you are using a session state server or SQL session, session_end does not fire.

When using in process session, session_end only fires if you have stored something in session. If you aren't using session, session_end will never fire.

If you are using session and the user closes the browser, session_end will fire when the users session times out. While it will have some of the same settings as the original session, this event will not be tied to a browser since it is the worker process detecting the session timeout and firing the session_end process.

Jeff Siver