Click here to Skip to main content
Click here to Skip to main content

How to end user session when browser closed

By , 8 Feb 2011
 

Introduction

 

How to capture logoff time when user closes browser?

 

Or

 

How to end user session when browser closed?

 

Or

 

How to end user session when user redirects to another site?

 

These are some of the frequently asked questions. Normally this is the requirement of any application. There is no fool-proof technique to catch the browser close event for 100% of the time. The trouble lies in the stateless nature of HTTP. I am explaining one of them which is very effective and tested.

 

Using the Code

 

1. First create a page LogOut.aspx and in Page_Load event, write this code:

 
protected void Page_Load(object sender, EventArgs e)
{  
  Session.Abandon();
} 
 

2. Then add the following JavaScript code in your page or Master Page:

 
<script type="text/javascript">
 
var clicked = false;  
 function CheckBrowser()  
   {      
      if (clicked == false)   
         {      
          //Browser closed   
         }        else  
          {  
          //redirected
             clicked = false; 
           } 
   }  
  function bodyUnload() 
   {      
      if (clicked == false)//browser is closed  
          {   
         var request = GetRequest();  
           request.open  ("GET", "../LogOut.aspx", true);    
       request.send();    
        } 
   } 
 
   function GetRequest()  
     {       
     	var xmlHttp = null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		//Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
      } 
 
</script>
 

3. Add the following code in the body tag of master page:

 
<body onunload="bodyUnload();" Onclick="clicked=true;">
 

Finally the code in Master page like this:

 
<script language="text/javascript"
//<![CDATA[</span />

var clicked = false;
function CheckBrowser() {
if (clicked == false) {
//Browser closed
}
else {
//redirected 
clicked = false;
}
}
 
function bodyUnload() {
if (clicked == false)//browser is closed
{
//var request = GetRequest();

//location.href="/KB/tips/LogOut.aspx";
var request = GetRequest();
 
request.open("GET", "../LogOut.aspx", true);
request.send();
}
}
function GetRequest() {
	var xmlHttp = null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		//Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
} 
 
//]]>
</script;"
 
<body onunload="bodyUnload();" onclick="clicked=true;">
<form id="form1" runat="server">

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

M.Farrukh Abbas
Web Developer
Pakistan Pakistan
Member
I have worked on number of technologies including C# .Net, VB .Net, ASP.Net, LINQ, WCF, X++, and SharePoint,Oracle,Crystal Reports.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralSo, you've a function "CheckBrowser" which, if "clicked" is ...memberDerekTP12314 Feb '11 - 1:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130513.1 | Last Updated 9 Feb 2011
Article Copyright 2011 by M.Farrukh Abbas
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid