When I was unable to access the Internet in school, I changed the blog program and added a blog online count. Before this, I also read several codes that count online users. I felt that they did not meet my requirements, so I wrote one myself in combination with the blog program and applied it to my blog.
Some methods for ASP counting the number of online users are mentioned in Fdream's blog. For details, please see here.
In order to balance accuracy and efficiency, I used a passive approach to count online people, that is, update the number of online people when there are new visitors. I don’t know if anyone has used this method on the Internet, but I am using it now exactly what I think ^_^.
Session is used in L-Blog to determine whether a visitor is a new visitor. The timeout time of the Session is generally 20 minutes, which can be used to count the number of people online, that is, visitors who are active within 20 minutes think are online.
There is the following code in common.asp to record access records:
The code copy is as follows:
'Guest_IP is the guest's IP
IFSession("GuestIP")<>Guest_IPThen
DimGuest_Agent,Guest_Refer
'Guest_Agent and Guest_Refer access records should be used
Guest_Agent=Trim(Request.ServerVariables("HTTP_USER_AGENT"))
Guest_Refer=Trim(Request.ServerVariables("HTTP_REFERER"))
'Add an access record
Conn.ExeCute("INSERTINTOblog_Counter(coun_IP,coun_Agent,coun_Refer)VALUES
('"&Guest_IP&"','"&Guest_Agent&"','"&Guest_Refer&"'))
'Add 1 visit
Conn.ExeCute("UPDATEblog_InfoSETblog_VisitNums=blog_VisitNums1")
SQLQueryNums=SQLQueryNums2
'Save the guest IP with Session
Session("GuestIP")=Guest_IP
EndIF
In order to achieve online user statistics, I made some changes in the database:
1. Added 1 field blog_OnlineNums (integer) to the table blog_Info, to save the current number of online users of the website.
2. Added table blog_Onine, and the fields are set to: ol_ID (auto number), ol_IP (character, 20), ol_Time (date/time, default value Now).
The modified site statistics code is as follows:
The code copy is as follows:
'Site statistics code
IFSession("GuestIP")<>Guest_IPThen
'Original site access counter
Conn.ExeCute("INSERTINTOblog_Counter(coun_IP,coun_Agent,coun_Refer)
VALUES('"&Guest_IP&"','"&Guest_Agent&"','"&Guest_Refer&"')")
Conn.ExeCute("UPDATEblog_InfoSETblog_VisitNums=blog_VisitNums+1")
SQLQueryNums=SQLQueryNums+2
Session("GuestIP")=Guest_IP
'Online number of people
'Just determine whether the visitor has timed out in the online list