We help IT Professionals succeed at work.

Browser close redirect?

rmartes
rmartes asked
on
Hey Guys,

I have a web app that allows users to login and logout. When the user logs in, the login date/time is recorded in the database. When the user properly logs out (logout button), the logout date/time is also recorded into the db.

Accidently, users might click the X button on the browser and their logout times are not recorded.

How can I detect browser/window close through javascript? OR detect if the user session has been abandoned through ASP.NET VB? so I can record their logout times.
Comment
Watch Question

Chief Technology Ninja
Distinguished Expert 2019
Commented:
Hi rmartes,
You can call <body onunload="RecordLogOutTime()">
Where RecordLogOutTime could be a call to a page which can record current user's name and log out time.
For Session time out
Add a Global.asax file to your website, and in the Session_End event
protected void Session_End(Object sender, EventArgs e)
{
    // Record current user and record log out time
}

Regards,
Chinmay


Author

Commented:
Thanks Chinmay for the reply. Lets take the session time out approach because onunload doesn't always work the way I want it to. Can you walk me through adding the global file and the session_end event?
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Actually I've already described the steps you need to take. The code you have been using to log details of a session end should go where I've left comments.

Author

Commented:
Ok, tried it the global.asax file, I didn't know it was that easy...

Is Application_End or Session_End called when the browser is closed?

OR are they only called when the session is ended by something like session.abandon?
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
No. Go For Session_End. It will be called whenever by any means if a session is ended.
Application_End is when your IIS AppPool recycles or IIS itself is reset etc. 
Global.asax is a very nice feature, I remember playing with it a lot in classic ASP via creating ISAPI filters. Now it has been simplified. 

Author

Commented:
Thanks, one more question before I accept your solution...

I use session variables for storing user ids and other user info. When the user logins, there time in is recorded based on where userid = session(user_id). Can you use this same session variable in the global.asax? or are all session variables killed before calling session_end?
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
You are welcome. Kindly check this.Session and let me know. I have not implemented this recently so I might not have clear idea on this. But this.Session should provide you some information on Session. Also you should keep that unload function as well 'cause Browser close is not denoted by the server as a session end.

Author

Commented:
Right, I was able to use the session varables within the session_end function. I will have to still use the unload event to handle browser close.

Author

Commented:
Great Help! Thanks