Asp.net| Statistics | online
Statistics online users of the role is self-evident, is to site managers can know the current number of users, and then according to the number of users to observe the performance of the server or program, so you can intuitively understand the attractiveness of the site or the efficiency of the site program. Now, let's introduce a simple and straightforward way to count the number of online users, The feature of this method is to make full use of the characteristics of ASP.net, combined with Global.asax file, with application and session cleverly realize the statistics of online users, because the program only use a application, so, the program occupies system resources can almost ignore, of course, this also is one of the most concerned questions of the website manager.
One, the user displays the page the use
First of all, let's look at how realistic the number of visitors to the current site, the program code is as follows:
<%@ Page language= "VB" debug= "true"%>
<script language= "VB" runat= "Server" >
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
Visitors.text = "This site currently has: <b>" & (Application ("User_sessions"). ToString ()) & "" & _
"</b> Visitor!"
End Sub
</script>
<title> Online Users </title>
<body>
<asp:label id= "Visitors" runat= "Server"/><br>
</body>
It can be seen that the above program is particularly simple, is called application. Of course, we do not have to specifically design a page to display the number of online users, on any page of the site, we can directly call application ("User_sessions"). ToString () to display the current number of users.
Second, Global.asax file implementation
The role of the Global.asax file we don't have to say, now, let's see how the current number of online users can be achieved:
<script language= "VBScript" runat= "Server" >
Sub Application_OnStart (Sender as Object, E as EventArgs)
Application ("user_sessions") = 0
End Sub
Sub Application_OnEnd (Sender as Object, E as EventArgs)
End Sub
Sub Session_OnStart (Sender as Object, E as EventArgs)
Application.Lock ()
Application ("user_sessions") = Application ("user_sessions") + 1
Application.UnLock ()
End Sub
Sub Session_OnEnd (Sender as Object, E as EventArgs)
Application.Lock ()
Application ("user_sessions") = Application ("User_sessions")-1
Application.UnLock ()
End Sub
</script>
The above code is easy to understand, when the site started service (application start), the program set application ("User_sessions") to zero, and then, when the user entered the site (session began), Lock application, then, application ("User_sessions") plus one, when the user exits the site, application ("User_sessions") minus one. In this way, it is very clever to achieve the statistics of online users.
31 Point Discussion
The above statistics, concise, the program is easy to achieve. However, if we think about it, we find that the method has some limitations, the number of online users can be a little bit of error. Because we are in the above program, is based on the user to establish and exit the session to achieve the number of online addition and subtraction, and we know that if the user did not close the browser, and access to another site, then, this session in a certain period of time will not end, This time we can set by timeout, general, we set to 20 minutes. Therefore, in the number of users, there is still a little error.
In addition, we know that in the ASP, if the user to the browser above the cookies set to "disabled", then the session can no longer be passed, obviously, this set up so that the above statistical procedures powerless. However, in the asp.net we have a workaround, in the Config.web file, we will be <sessionstate cookieless= "false"/> set to true, it is said, You can also pass the session without using cookies. In this way, our programs can run smoothly in different visitor environments.
Iv. Summary
The above statistical procedures are particularly simple, but the details of things we do not necessarily think of, this is also in the programming we need a little more consideration.