Source: Tianji Forum
It is self-evident to count online users so that website managers can know the number of current users, and then observe the server or Program So that you can intuitively understand the attractiveness of the website or the efficiency of the website program. 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. asax file, with application and
Session cleverly implements 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 most important concerns of website administrators.
1. Use of the User display page
First, let's take a look at how the number of users accessing the current website, the program Code 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.