The purpose of online user statistics is self-evident in that website managers can know the number of current users,
Then observe the server orProgramSo that you can intuitively understand the website's performance
Gravity or website program efficiency. Now, we will introduce a simple and clear method to count online users.
The feature of this method is to make full use of the features of ASP. NET, combined with the global. asax file,
The application and session are used to skillfully implement online user statistics, because only one
Application.
This is one of the most important issues.
1. Use of the User display page
First, let's take a look at how the number of users accessing the current website, the programCodeAs 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> visitor! ";
}
<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
A page is specially designed 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 take a look at the current online usage statistics.
How to Implement the number of households:
<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 (Session
At the beginning), lock the application, and then (
"User_sessions") 1. 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 think about it carefully, we find that
The method has some limitations, and the number of online users may be slightly different. Because we are above
In the program, the number of online users can be added or subtracted based on the user's session creation and exit.
We know that if the user does not close the browser and enters another website, the session is
The time will not end. We can use timeout to set this time. In general, we set
Set 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, we have a solution in ASP. NET. In the config. Web file, we will <
Sessionstate cookieless = "false"/> is set to true.
Cookies can also be transferred to sessions. In this way, our program can be smooth in different visitor Environments
Run.
Iv. Summary
The above statistical procedures are very simple, but we do not necessarily think of details.
Programming requires more consideration.
-----------------------------------------------------------------------
The specific principle is:
When the application starts, it reads numbers from a file and puts them into an application,
This mainly prevents resetting when the site is restarted.
Then there is session_start, variable + 1,
Update the data again at application_end,
The Code is as follows:
<% @ Application language = "C #" %>
<% @ Import namespace = "system. Data. sqlclient" %>
<SCRIPT runat = "server">
Void application_start (Object sender, eventargs E)
{
// Code that runs when the application starts
Sqlconnection con = new sqlconnection ();
Con. connectionstring = configurationmanager. connectionstrings ["tongjiconnectionstring"]. connectionstring;
Con. open ();
Sqlcommand cmd = new sqlcommand ("select * From liul1_gji", con );
Int COUNT = convert. toint32 (CMD. executescalar ());
Con. Close ();
Application ["Total"] = count;
}
Void application_end (Object sender, eventargs E)
{
// Code that runs when the application is closed
Sqlconnection con = new sqlconnection ();
Con. connectionstring = configurationmanager. connectionstrings ["tongjiconnectionstring"]. connectionstring;
Con. open ();
Sqlcommand cmd = new sqlcommand ("Update liul1_gji set totalnum =" + application ["Total"]. tostring (), con );
Cmd. executenonquery ();
Con. Close ();
}
Void application_error (Object sender, eventargs E)
{
// Code that runs when an unhandled error occurs
}
Void session_start (Object sender, eventargs E)
{
Session ["admin"] = NULL;
Session ["admintype"] = NULL;
// The code that runs when the new session starts
Application. Lock ();
Application ["Total"] = convert. toint32 (application ["Total"]) + 1;
Application. Unlock ();
}
Void session_end (Object sender, eventargs E)
{
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the web. config file is set
// The session_end event is triggered only when inproc is used. If the session mode is set to StateServer
// Or sqlserver, the event is not triggered.
}
</SCRIPT>