Click here to Skip to main content
14,662,169 members
Articles » General Programming » Programming Tips » General
Tip/Trick
Posted 7 Feb 2011

Stats

204.7K views
34 bookmarked

How to end user session when browser closed

Rate this:
4.82 (26 votes)
Please Sign up or sign in to vote.
4.82 (26 votes)
8 Feb 2011CPOL
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>

<body onunload="bodyUnload();" onclick="clicked=true;"><span style="font-size: 9pt;">  </span>
<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

Comments and Discussions

 
QuestionCrisis averted Pin
Member 1449430311-Jun-19 2:25
MemberMember 1449430311-Jun-19 2:25 
QuestionNot Working Logout.aspx Pin
Member 1365131511-Feb-18 20:15
MemberMember 1365131511-Feb-18 20:15 
QuestionHo Do I Use Without Refresh Pin
Member 113230896-Nov-17 2:33
MemberMember 113230896-Nov-17 2:33 
BugSession abandon when refresh and back button Pin
Member 1346945116-Oct-17 22:07
MemberMember 1346945116-Oct-17 22:07 
QuestionChrome and Firefox Pin
Nelson634020-Jun-17 10:42
MemberNelson634020-Jun-17 10:42 
QuestionQuestion regarding sample code Pin
Kashif Ansari 12312-Sep-15 23:17
MemberKashif Ansari 12312-Sep-15 23:17 
QuestionPoint 2 Pin
Karan Chhabria26-Aug-15 22:02
MemberKaran Chhabria26-Aug-15 22:02 
AnswerRe: Point 2 Pin
M.Farrukh Abbas28-Aug-15 2:02
MemberM.Farrukh Abbas28-Aug-15 2:02 
BugNot working for page referesh and ispostback Pin
kinal371-Jul-15 8:50
Memberkinal371-Jul-15 8:50 
GeneralRe: Not working for page referesh and ispostback Pin
M.Farrukh Abbas28-Aug-15 2:05
MemberM.Farrukh Abbas28-Aug-15 2:05 
GeneralRe: Not working for page referesh and ispostback Pin
Member 1339602015-Nov-18 1:23
MemberMember 1339602015-Nov-18 1:23 
QuestionQuestion for all browser Pin
mayur_man3-Mar-15 15:38
Membermayur_man3-Mar-15 15:38 
AnswerRe: Question for all browser Pin
M.Farrukh Abbas6-May-15 23:29
MemberM.Farrukh Abbas6-May-15 23:29 
Question1 Doubt Pin
jakss1-Mar-15 19:02
Memberjakss1-Mar-15 19:02 
QuestionFirefox Pin
Member 109217103-Jul-14 2:16
MemberMember 109217103-Jul-14 2:16 
QuestionHow to end user session when browser closed Pin
basitsar1-Dec-13 0:27
Memberbasitsar1-Dec-13 0:27 
AnswerRe: How to end user session when browser closed Pin
Member 269551312-Dec-13 14:18
MemberMember 269551312-Dec-13 14:18 
GeneralMy vote of 5 Pin
shaziafazili18-Dec-12 6:47
Membershaziafazili18-Dec-12 6:47 
QuestionWoW Pin
gfsrdgehfhfd19-Jul-12 4:09
Membergfsrdgehfhfd19-Jul-12 4:09 
AnswerRe: WoW - But Pin
gfsrdgehfhfd19-Jul-12 4:56
Membergfsrdgehfhfd19-Jul-12 4:56 
Questiongood trick but one bug Pin
y_srikanth21-Jun-12 5:12
Membery_srikanth21-Jun-12 5:12 
GeneralMy vote of 5 Pin
Rahul Rajat Singh22-May-12 18:10
professionalRahul Rajat Singh22-May-12 18:10 
QuestionPerfect code Pin
Member 379524815-May-12 23:58
MemberMember 379524815-May-12 23:58 
QuestionBrowser Close Event Pin
Member 856404529-Mar-12 20:31
MemberMember 856404529-Mar-12 20:31 
GeneralReason for my vote of 1 Wtf Pin
johannesnestler5-Feb-12 21:16
Memberjohannesnestler5-Feb-12 21:16 

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.