Click here to Skip to main content
12,635,398 members (24,925 online)
Click here to Skip to main content
Add your own
alternative version

Stats

132.2K views
34 bookmarked
Posted

How to end user session when browser closed

, 8 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
There is no fool-proof technique to catch the browser close event for 100% of time. The trouble lies in the stateless nature of HTTP. I will explain one of them which is very effective and tested

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.

I updated the article and now it support all kind of browser .

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  ("POST", "../LogOut.aspx", false);    
       request.send();    
        } 
   } 
 
   function GetRequest()  
     {       
     var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            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="javascript" type="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();

            request.open("POST", "../LogOut.aspx", false);
            request.send();
            alert('This is close');
        }
    }
    function GetRequest() {
        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        return xmlhttp;
} 
</script>
<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)

Share

About the Author

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

http://dotnetfarrukhabbas.blogspot.com

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionQuestion regarding sample code Pin
Kashif Ansari 12313-Sep-15 0:17
memberKashif Ansari 12313-Sep-15 0:17 
QuestionPoint 2 Pin
Karan Chhabria26-Aug-15 23:02
memberKaran Chhabria26-Aug-15 23:02 
AnswerRe: Point 2 Pin
M.Farrukh Abbas28-Aug-15 3:02
memberM.Farrukh Abbas28-Aug-15 3:02 
BugNot working for page referesh and ispostback Pin
kinal371-Jul-15 9:50
memberkinal371-Jul-15 9:50 
GeneralRe: Not working for page referesh and ispostback Pin
M.Farrukh Abbas28-Aug-15 3:05
memberM.Farrukh Abbas28-Aug-15 3:05 
QuestionQuestion for all browser Pin
mayur_man3-Mar-15 16:38
membermayur_man3-Mar-15 16:38 
AnswerRe: Question for all browser Pin
M.Farrukh Abbas7-May-15 0:29
memberM.Farrukh Abbas7-May-15 0:29 
Question1 Doubt Pin
jakss1-Mar-15 20:02
memberjakss1-Mar-15 20:02 
QuestionFirefox Pin
Member 109217103-Jul-14 3:16
memberMember 109217103-Jul-14 3:16 
QuestionHow to end user session when browser closed Pin
basitsar1-Dec-13 1:27
memberbasitsar1-Dec-13 1:27 
AnswerRe: How to end user session when browser closed Pin
Member 269551312-Dec-13 15:18
memberMember 269551312-Dec-13 15:18 
GeneralMy vote of 5 Pin
shaziafazili18-Dec-12 7:47
membershaziafazili18-Dec-12 7:47 
QuestionWoW Pin
gfsrdgehfhfd19-Jul-12 5:09
membergfsrdgehfhfd19-Jul-12 5:09 
AnswerRe: WoW - But Pin
gfsrdgehfhfd19-Jul-12 5:56
membergfsrdgehfhfd19-Jul-12 5:56 
Questiongood trick but one bug Pin
y_srikanth21-Jun-12 6:12
membery_srikanth21-Jun-12 6:12 
GeneralMy vote of 5 Pin
Rahul Rajat Singh22-May-12 19:10
memberRahul Rajat Singh22-May-12 19:10 
QuestionPerfect code Pin
Member 379524816-May-12 0:58
memberMember 379524816-May-12 0:58 
QuestionBrowser Close Event Pin
Member 856404529-Mar-12 21:31
memberMember 856404529-Mar-12 21:31 
GeneralReason for my vote of 1 Wtf Pin
johannesnestler5-Feb-12 22:16
memberjohannesnestler5-Feb-12 22:16 
GeneralReason for my vote of 4 it is a fantastic and helpful articl... Pin
NileshKRathod14-Oct-11 1:09
memberNileshKRathod14-Oct-11 1:09 
GeneralNot a complete solution: 'unload' event fires every time use... Pin
sjelen18-Feb-11 5:35
membersjelen18-Feb-11 5:35 
GeneralReason for my vote of 1 Does not do what it says on the tin.... Pin
DerekTP12314-Feb-11 2:53
memberDerekTP12314-Feb-11 2:53 
GeneralSo, you've a function "CheckBrowser" which, if "clicked" is ... Pin
DerekTP12314-Feb-11 2:52
memberDerekTP12314-Feb-11 2:52 
GeneralHow this classifies as a "Article" is beyond me.... Totally ... Pin
jfriedman8-Feb-11 13:48
memberjfriedman8-Feb-11 13:48 
GeneralRe: It's not, it is a tip/trick. Perfectly acceptable (after fix... Pin
Indivara8-Feb-11 15:51
subeditorIndivara8-Feb-11 15:51 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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