The role of statistics online users is self-evident, which is to know how much the current user is, and then observe the performance of the server or program according to the number of users, so it can be intuitive to understand the attraction of the website or the efficiency of the website program. Now, we will introduce a simple and clear way to count how much online users. The characteristics of this method are to fully utilize the characteristics of ASP.NET, combined with the Global.asax file, using Application and Session to realize online users, Since only one Application is used in the program, the program occupies system resources can almost neglect, of course, this is also one of the most concerned issues of website managers. First, the user shows the use of the page
First, let's take a look at how the number of access users of the current website, the program code is as follows:
<% @ Page language = "c #" debug = "true"%>
Private Void Page_Load (Object Sender, System.EventArgs E)
{
Visitors.Text = "This site is currently: " Application ["user_ssssions"]. TOSTRING () " " <"<";
}
HEAD>
Body>
Html>
It can be seen that the above procedures are particularly simple, that is, call Application. Of course, we don't have to design a page to display the number of online users, any page of the website, we can directly call Application ("User_SESSIONS"). ToString () to display the current number of users.
Second, the global.asax file implementation
The role of the global.asax file We don't have to say, now we are directly to see how the current online user is implemented:
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_ssions"] 1; application.unlock ();
}
Protected void session_end (Object Sender, Eventargs E)
{
Application.lock ();
Application ["User_SESSIONS"] = (int) Application ["user_ssions"] - 1;
Application.unlock ();
}
Script>
The above code is easy to understand, when the website starts service (when Application Start), the program sets Application ["User_SESSIONS"] is zero, then, when the user enters the website (when the session begins), lock Application, then, then, Add Application ("User_SESSIONS"), when the user exits the website, Application ("User_SESSIONS") is reduced. In this way, it is very clever to realize the statistics of online users.
Third, a little discussion
The above statistics, concise, and the procedure is easy to implement. However, if we carefully consider, it is found that this method has certain limitations, and the number of online users may be slightly a bit error. Because we are in the above programs, we have subtracted from the number of people to the number of people based on the user, and we know that if the user does not close the browser, then enter another website, then this session is in certain Time will not end, this time we can set it through Timeout, general, we set it 20 minutes. Therefore, at the quantity statistics, there is still a little error.
Also, we know that in the ASP, if the user sets the cookies above the browser to "disable", then session can no longer be passed, obviously, this setting will make the above statistical procedure. However, in ASP.NET, we have a solution. In the config.Web file, we set
Fourth, summary
The above statistical procedures are particularly simple, but the details we don't necessarily think that this is also what we need to consider in programming.