In asp.net, determine when and how the user leaves

Source: Internet
Author: User

Website user statistics are useful for each site. However, few people make statistics on online users absolutely accurate or relatively accurate. Based on the Web principles, the server does not take the initiative to know that the user leaves the site by closing the browser. At this time, the user can only rely on the server variable Session to determine whether the user leaves, but the user's browser can know when the user closes, the Code is also very simple

As follows:

Function body_onunload ()

{

If (window. event. clientX <0)

{

Alert ('the broswer is closing ...');

}

Else

{

Alert ('the user is refreshing or navigating away ...);

}
}

The above code will determine whether the user is closing the browser or refreshing or navigating to other pages!


Bytes ---------------------------------------------------------------------------------------------------
In the afternoon, I tested it with code, mainly using the above js functions and ajax technology. I tried it three times before and after because I didn't have a thorough understanding of ajax technology.
I will share the results with you and give you more comments.
First, I tried to access the Application in the ajax method and tried to use the Application variable to record the number of online users. Try code:
[AjaxPro. AjaxMethod]
Public int Logoff ()
{
Application ["onlineCount"] = Convert. ToInt32 (Application ["onlineCount"])-1;
Return Convert. ToInt32 (Application ["onlineCount"]);
}
Failed to try! It is estimated that ajax cannot access the static variables and Application variables of the Application. The first time I used AjaxPro, I don't know how to set the Access Session. I used ajax. dll and set it to read/write sessions. The attempt result may change!
Second, I tried to use a text file to record the number of online users, but still failed!
Third, use the database! The attempt is successful.
The Code is as follows:
[AjaxPro. AjaxMethod]
Public int Logoff ()
{
SqlConnection conn = new SqlConnection ("server = ..; database = test; uid = sa; pwd = 123 ");
SqlCommand cmd = new SqlCommand ("update OnlineCount set num = num-1", conn );
Try
{
Conn. Open ();
Cmd. ExecuteNonQuery ();
}
Catch (Exception ex)
{
EventLog. WriteEntry (ex. Source, ex. Message );
}
Finally
{
Conn. Close ();
}

Cmd = new SqlCommand ("select top 1 num from OnlineCount", conn );
Try
{
Conn. Open ();
Return Convert. ToInt32 (cmd. ExecuteScalar ());
}
Catch (Exception ex)
{
EventLog. WriteEntry (ex. Source, ex. Message );
}
Finally
{
Conn. Close ();
}
Return 0;

}
Front-end page shrinking


1 <script language = "javascript">
2 <! --
3 function leave ()
4 {
5 alert (window. event. clientX );
6 if (window. event. clientX <0)
7 {
8 WebApplication3.WebForm1. Logoff (). value;
9}
10}
11 // -->
12 </script>
13 </HEAD>
14 <body onunload = "leave ()">
In this way, when the security line and timeliness requirements are strict, set a page as the primary page. If the page is closed, the user has exited. This method can be used by the email system.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.