Passive Statistics Site Online number _ Application Tips

Source: Internet
Author: User
Tags servervariables trim

In the school can not surf the internet, nothing to do to change the next blog program, plus a blog online count. Before this also looked at several statistics online number of code, the feeling is not too in line with their requirements, on the combination of blog program himself wrote a, applied to my blog .

In Fdream blog mentions some about ASP statistics online number of methods, can be seen here.

To balance accuracy and efficiency, I used a passive way of counting people who were online when they had new visitors. I do not know whether the Internet has been using this method, but I now use is entirely my own thinking of the ^_^.

In L-blog, a session is used to determine whether a visitor is a new visitor. The session timeout is typically 20 minutes, which can be used to count online numbers, that is, the 20-minute event visitor thinks it is online.

The following section of code is used to record access records in commond.asp:

Copy Code code as follows:

' Guest_ip for the IP of the visitor
IF session ("Guestip") <>guest_ip Then
Dim Guest_agent,guest_refer
' Guest_agent and Guest_refer access records are used
Guest_agent=trim (Request.ServerVariables ("Http_user_agent"))
Guest_refer=trim (Request.ServerVariables ("Http_referer"))
' Add an Access record
Conn.execute ("INSERT into Blog_counter (coun_ip,coun_agent,coun_refer) VALUES
(' &Guest_IP& ', ' &Guest_Agent& ', ' ' "&Guest_Refer& ')")
' Number of visits plus 1
Conn.execute ("UPDATE blog_info SET blog_visitnums=blog_visitnums 1")
Sqlquerynums=sqlquerynums 2
' Save guest IP with session
Session ("Guestip") =guest_ip
End IF

In order to achieve online demographics, I have made some changes in the database:

1. Added 1 field blog_onlinenums (integer) in table Blog_info to save the current number of Web sites online

2. Added table Blog_onine, field set to: ol_id (AutoNumber), ol_ip (characters,), ol_time (Date/time, default value now).

The revised site statistics code is as follows:
Copy Code code as follows:

' Site Statistics code
IF session ("Guestip") <>guest_ip Then
' Original site access counter
Conn.execute ("INSERT into Blog_counter" (Coun_ip,coun_agent,coun_refer)
VALUES (' &Guest_IP& ', ' "&Guest_Agent&" ', ' "&Guest_Refer& ')")
Conn.execute ("UPDATE blog_info SET blog_visitnums=blog_visitnums+1")
Sqlquerynums=sqlquerynums+2
Session ("Guestip") =guest_ip
' Online headcount statistics
' Determine if a visitor has timed out in the online list
IF conn.execute ("Select COUNT (ol_id) from Blog_online
WHERE DateDiff (' n ', Ol_time,now ()) >20 ") (0) >0 Then
' If so, overwrite a record of a guest who has timed out
Conn.execute ("UPDATE blog_online SET ol_ip= '" &Guest_IP& ",
Ol_time=now () WHERE ol_id in (SELECT top 1 ol_id
From Blog_online WHERE DateDiff (' n ', Ol_time,now ()) >20) "
Else
' If not, add an online visitor record
Conn.execute ("INSERT into Blog_online" (OL_IP)
VALUES (' "&Guest_IP&") ")
End IF
' Statistics blog Online number
Blog_onlinenums=conn.execute ("Select DISTINCT COUNT (ol_id)
From Blog_online WHERE DateDiff (' n ', Ol_time,now ()) <20 ") (0)
' Update the number of online blog information
Conn.execute ("UPDATE blog_info SET blog_onlinenums=" &blog_OnlineNums& ")
Sqlquerynums=sqlquerynums+3
End IF

In this way, in the blog need to show the number of online reference variable blog_onlinenums on the line.

And because it is a passive statistic, the impact on the blog page execution time is almost negligible, in addition, this method statistics blog online number also has a certain accuracy, can meet the general needs.

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.