c# - Killing session value in Browser Close - Stack Overflow

I am working in C#.Net. I want to kill the session values when the browser is closed. (i.e) In my appli

I am working in C#.Net. I want to kill the session values when the browser is closed. (i.e) In my application, i want to display online visitors count. If the user clicks logout button means, it works fine. rather than if he close the browser, the session value not cleared.

if the browser close is done, the session value should be killed....

My Global.ascx code...

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup      
    Application["OnlineUsers"] = 0;
}

void Application_End(object sender, EventArgs e)
{
    //  Code that runs on application shutdown
}

void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
}

void Session_Start(object sender, EventArgs e)
{
    Application.Lock();
    Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
    Application.UnLock();
}

void Session_End(object sender, EventArgs e)
{
    Application.Lock();
    Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
    Application.UnLock();
}

My Home.aspx code...

 Visitors online: <%= Application["OnlineUsers"].ToString() %>

If i run my application in IE, the online count will be 1. If i runs in Firefox, the online count should be incresed to 2. If i close the Firefox browser, the count in IE should be changed to 1.

This is my Requirement....

I am working in C#.Net. I want to kill the session values when the browser is closed. (i.e) In my application, i want to display online visitors count. If the user clicks logout button means, it works fine. rather than if he close the browser, the session value not cleared.

if the browser close is done, the session value should be killed....

My Global.ascx code...

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup      
    Application["OnlineUsers"] = 0;
}

void Application_End(object sender, EventArgs e)
{
    //  Code that runs on application shutdown
}

void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
}

void Session_Start(object sender, EventArgs e)
{
    Application.Lock();
    Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
    Application.UnLock();
}

void Session_End(object sender, EventArgs e)
{
    Application.Lock();
    Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
    Application.UnLock();
}

My Home.aspx code...

 Visitors online: <%= Application["OnlineUsers"].ToString() %>

If i run my application in IE, the online count will be 1. If i runs in Firefox, the online count should be incresed to 2. If i close the Firefox browser, the count in IE should be changed to 1.

This is my Requirement....

Share Improve this question edited Aug 24, 2011 at 10:54 RobinHood asked Aug 24, 2011 at 10:18 RobinHoodRobinHood 2,40711 gold badges50 silver badges74 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

The Http protocol is a connectionless one, so unless you are planning to use websockets, the only two ways to kill the user session should be by a direct request by the user (i.e. pressing a logout link), or a session timeout.

You should not rely on the browser to kill the session. The user can forget to close the browser, or he may have javascript disabled. Instead, provide means to the user to close the session and set your session timeout to a reasonable value adjusted to your users activity.

if (!Session.IsNewSession && Request.UrlReferrer == null)
{

 }

if we type or paste url, this code will redirect to login page

You could trigger javascript, like this:

<SCRIPT language="JavaScript">
<!--
function loadOut()
{
    window.location="http://www.yoursite./loadout.php?sid=235346317";
}
//-->
</SCRIPT>

<body onBeforeUnload="loadOut()">

Your javascript could perform an AJAX call, or whatever. This will also work, I think, when the user leaves your site.

Is this what you were searching for?

How about this:

<body onunload="doUnload()">

Then the javascript, would be something like:

function doUnload()
{
   $.post("unset_session.php", { action: "unset" },
         function(data) {
         // Callback if wanted
    });
}

Then in your php, just unset the session data, or destroy the session

<?php
if ($_POST['action'] == 'unset')
{
     // Unset or Destroy the Session
     session_destroy();
}

?>

I did this in my app for IE, when user click browser right up corner X button, I want to force user logout. Hope that helps.

In master page:

 <script type="text/javascript">
    window.onbeforeunload = function (e) {
        //alert("Killing the session on the server!!");
        if ((window.event.clientX < 0) || (window.event.clientY < 0)) {
            document.getElementById('<%= FORCE_LOGOUT_BTN.ClientID %>').click();
        }
    } 
</script>

In site.master page:

<asp:Button ID="FORCE_LOGOUT_BTN" runat="server" CssClass="noShow"OnClick="ForceLogOut" Width="10px" />

Have to use CssClass, set Visible=false not working, since object will bee null. Css noShow is: visibility:hidden;

In site.master.cs

public void ForceLogOut(object sender, EventArgs e)
{
    FormsAuthentication.SignOut(); // since I use Form authentication
    Session.Abandon();
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745171795a4614970.html

相关推荐

  • c# - Killing session value in Browser Close - Stack Overflow

    I am working in C#.Net. I want to kill the session values when the browser is closed. (i.e) In my appli

    6小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信