Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Session_End fire on window close? (ASP.NET)

I am putting an "online" counter on a website, and I have run into these two contradicting sources.

This one (I am using this example code):

http://aspdotnetfaq.com/Faq/How-to-show-number-of-online-users-visitors-for-ASP-NET-website.aspx

...says that:

Also when user closes his browser or does not click on any links in our website, session expires, and our "OnlineUsers" global variable is decreased.

However, this one:

http://www.velocityreviews.com/forums/t383710-session-end-guarantee.html

...says that:

Closing the browser window, or browsing to another site will NOT cause Session_End to fire, at least, not straightaway - the server has absolutely no way of knowing what happens on the client machine until it gets another HttpRequest from it. In this instance, Session_End will fire when the session times out naturally.

I have tested and it seems that Session_End DOES NOT fire.

I basically want you guys to confirm or comment on this.

Is it possible to update the online counter on browser-close?

like image 726
Jesper Avatar asked Dec 16 '22 08:12

Jesper


2 Answers

The second is true

Closing the browser window, or browsing to another site will NOT cause Session_End to fire, at least, not straightaway - the server has absolutely no way of knowing what happens on the client machine until it gets another HttpRequest from it. In this instance, Session_End will fire when the session times out naturally.

Session time out is 20 minutes by default. You can confirm this by not doing any activity on your website for 20 min.

like image 53
Waqas Raja Avatar answered Dec 29 '22 15:12

Waqas Raja


Your two links are not contradictory. The first link is poorly worded, but it basically says what the second link says. It would be easier to understand if it were written like this:

Also when user closes his browser or does not click on any links in our website (after a period of time) the session will expire, and our "OnlineUsers" global variable is decreased.

Also note that Session_End may not always fire, especially if the session is killed or bounced (for example, if you update the web.config, the worker process recycles, or in some cases an uncaught exception occurs).

like image 31
Erik Funkenbusch Avatar answered Dec 29 '22 17:12

Erik Funkenbusch