Engaged in: http://myblog.workgroup.cn/blogs/aspnet/archive/2006/07/31/ASP.NET_2D4E2857BF7E28753762DF7EA18B_.aspx
The purpose of online user statistics is self-evident that website administrators can know the number of current users and observe the performance of servers or programs based on the number of users, in this way, you can intuitively understand the attractiveness of your website or the efficiency of your website programs. Now, we will introduce a simple and clear method to count the number of online users. The feature of this method is to make full use of ASP. NET, combined with global. the asax file uses Application and Session to skillfully implement online user statistics. Because only one Application is used in the program, the system resources occupied by the program can be ignored. Of course, this is also one of the top concerns of website administrators.
1. Use of the User display page
First, let's take a look at how the current number of users to access the website. The program code is as follows:
<% @ Page Language = "c #" debug = "true" %>
<Html>
<Head>
<Script language = "c #" RUNAT = "server">
Private void Page_Load (object sender, System. EventArgs e)
{
Visitors. Text = "this site currently has: <B>" + Application ["user_sessions"]. ToString () + "" + "</B> Visitors! ";
}
<Title> online users </title>
</Head>
<Body>
<Asp: label id = "visitors" runat = "server"/> <br>
</Body>
</Html>
It can be seen that the above program is particularly simple, that is, to call the Application. Of course, we do not need to design a page to display the number of online users. On any page of the website, we can directly call Application ("user_sessions "). toString () to display the number of current users.
II. Implementation of the global. asax File
We do not have to mention the role of the global. asax file. Now, let's look at how to count the number of online users:
<Script language = "c #" runat = "Server">
Protected void Application_Start (Object sender, EventArgs e)
{
Application ["user_sessions"] = 0;
}
Protected void Session_Start (Object sender, EventArgs e)
{
Application. Lock ();
Application ["user_sessions"] = (int) Application ["user_sessions"] + 1;
Application. Unlock ();
}
Protected void Session_End (Object sender, EventArgs e)
{
Application. Lock ();
Application ["user_sessions"] = (int) Application ["user_sessions"]-1;
Application. Unlock ();
}
</Script>
The above code is easy to understand. When the website starts to serve (when the Application starts), the Program sets Application ["user_sessions"] to zero. Then, when the user enters the website (when the Session starts), the Application is locked, and application ("user_sessions") is added. When the user exits the website, the application ("user_sessions ") minus one. In this way, the statistics of online users are cleverly implemented.
III. A little discussion
The above statistics are concise and easy to implement. However, if we carefully consider the limitations of this method, the number of online users may be slightly different. In the above program, we add or subtract the number of online users based on the user's Session creation and exit. We know that if the user does not close the browser, however, when you enter another website, the session will not end in a certain period of time. We can set this time through TimeOut. Generally, we set this time to 20 minutes. Therefore, there is still a slight error in the user quantity statistics.
In addition, we know that in ASP, if the user sets the Cookies on the browser to "disabled", the Session cannot be passed. Obviously, this setting makes the above statistical program powerless. However, in ASP. NET. in the web file, we can set <sessionstate cookieless = "false"/> to true. That is to say, the Session can be passed without using Cookies. In this way, our programs can run smoothly in different visitor environments.
Iv. Summary
The above statistical procedures are very simple, but we do not necessarily think of details. This is what we need to consider more in programming.