When building a website for online communication, there is a problem that gives me a headache, which is about real-time statistics of online users. The customer requires: counting the current number of people online, the number of visitors, the number of members, and the list of online users, including visitors, members and management. member (if it is a visitor, the visitor's ID is automatically generated, if it is a member, the member's name is displayed). Because it requires real-time performance, first I will pass the idea of using global.asa to solve it.
The key to the problem is how to determine that the user has left, and how to execute a file or a function when the user leaves.
After discussing with some friends on the Internet, I finally solved this problem.
The solution principle is: write a universal page. The so-called universal page means that every page in the application contains this page, for example: header.asp. In this page, write a piece of code using XMLHTTP. The function of this code is A request is sent to the server every 10 seconds or 20 seconds, with the purpose of updating the current user's online time and deleting users whose online time exceeds a certain time, so that the online user records in the database maintain a certain real-time nature.
The main implementation method is:
create a new database, the field names are: id (character), name (character), user (number) tt (date), admin (authority code, 0-normal user, 1-administrator)
table name :online
header.asp ↓
============================================== ================
<%
... ...
if session("s_in")<>1 and session("s_name")="" then 'If the user is the Log in once to
rs.open "select * from online",conn,3,3
rs.addnew
rs("id")=session.sessionID
rs("name")="Guest" & session.sessionID
rs("user")=0 '0 means the user is not logged in and is a visitor.
rs("tt")=now
rs.update
rs.close
session("s_in")=1 'Set the user's information has been stored in the database, indicating that it is online
end if
if session("s_name")<>"" then 'If the user has logged in through the login box
rs.open "select * from online where id='" & session.sessionID & "'",conn,3,3
rs("name")=session("s_name")
rs("admin")=session("s_admin") 'Update the user's name to the member name
rs("user")=1 'Indicates that the user has logged in and is a member
rs("tt")=now 'Set the current system time to the user's login time
rs.update
rs.close
end if
... ...
%>
... ...
<head>
... ...
<script language=javascript>
functionTest()
{
var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
xmlhttp.open("POST","onceonline.asp",false); //Send update request to onceonline.asp
xmlhttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
xmlhttp.send();
}
setInterval("Test();",10); //Send an update request every 10 seconds
</script>
... ...
</head>
... ...
============================== ============================
onceonline.asp
<%
rs.open "select tt from online where id='" & session.sessionID & "'",conn,3,3
rs("tt")=now() 'Update the online time of the current online user
rs.update
rs.close
rs.open "delete from online where datediff('s',tt,now())>60",conn,3,1 'Delete timeout user
%>
================================================ ==============
In this way, the real-time nature of the user list in the database is basically guaranteed. The error depends on the difference between the update time and the deletion time and the processing speed of the server. It is recommended not to timeout the deletion. If the time interval between users is too small, it may lead to errors among online users.
This solution has been debugged on WIN2000+SQL Server2000. Since this solution has relatively large system requirements, I hope other friends will come up with better solutions and solve this problem together!
My mailbox: [email protected]
My name is "Building Blocks", you are welcome to become friends with me!