Code for online website statistics

Source: Internet
Author: User

When browsing the web page, you can often see that the number of online users on the current website is XXX. How can I use ASP for one? First, let's analyze its practice. In general, these online statistics refer to the number of visitors in a period of time. For example, the length of the time (within 5 minutes, within 10 minutes) is set by the designer.
In this period, the total number of visitors from different IP addresses can be obtained. However, there is a precise problem to be solved. How can we accurately calculate the number of online visitors, that is to say, we have different practices. For example, we can use an automatically submitted page to read COOKIES over a period of time. In ASP, there is a better technique, the session object is used for statistics. Here we will introduce gobal. the asa file is a very important file. Please check its structure.
<Script language = "VBScript" RUNAT = "Server">
Sub Session_OnStart
End Sub
Sub Session_OnEnd
End Sub
Sub Application_OnStard
End Sub
Sub Application_OnEnd
End Sub
</SCRIPT>
When a session occurs (the user browses the Web page and sends a request to the Web server), the Application_OnStard and Session_OnStart events will occur for the first user after the server starts, then, if another user sends a request, only the Session_OnStart event will occur. The session duration can be set. timeout = X (minutes)
Well, with this very useful method, we can accurately calculate the number of online users, and the total number of people is saved using an application variable. When the first session starts, in the Application_OnStard event, place a statement application ("online") = 0 to clear the counter. Then, in the Session_OnStart event, place a statement to increase the number of online users.
Application ("online") = application ("online") + 1. In the Session_OnEnd event, place a statement to reduce the number of online users, reducing the count value by one.
In this way, the file is changed to the following:
<Script language = "VBScript" RUNAT = "Server">
Sub Session_OnStart
Application ("online") = application ("online") + 1
End Sub
Sub Session_OnEnd
Application ("online") = application ("online")-1
End Sub
Sub Application_OnStard
Application ("online") = 0
End Sub
Sub Application_OnEnd
Application ("online") = 0
End Sub
</SCRIPT>


Next, compile a program to display the number of online users on a specified page. In this way, the number of people can be obtained.
* Online. asp
<% @ Language = "vbscript" %>
<%
Tmp = application ("online ")
Tmp = Cstr (tmp)
Dim disp (20)
Dim images (20)
Dbbits = len (tmp)
For I = 1 to dbbits
Disp (I) = left (right (tmp, I), I-(i-1 ))
Next
For I = dbbits to 1 step-1
Images (I) = ""
Response. write "document. write ('" & images (I )&"');"
Next
%>


The above program is very simple. I will not analyze it here. Please read it by yourself. On the online statistics page, use a script to reference the online. asp file to display the graph statistician.
<Script language = "JavaScript" src = "http://xxxx.com.cn/online.asp"> </script>
Now, remember the key point. You must set gobal. the asa is placed in the correct web application root directory, and it is best to create a web application for this Statistics separately. Do not confuse it with other programs. Otherwise, the data obtained is inaccurate, modifying this program to multi-user online statistics is also very simple. In this way, you can provide the online statistics service. Please think about how to implement it ....




<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.