A solution that is currently online users can be used in the chat room, forum, website

zhaozj2021-02-16  54

When making an online communication site, there is a problem that I have a headache. It is about the problem of real-time statistics online users. Customer requirements: statistics, current online number, number of tourists, number of members, online users, including tourists, members, and management Revenner (if you are a tourist, you automatically generate the idler ID, if it is a member, the member name is displayed). Because it requires real-time, first I will drop the idea pass from Global.asa.

The key to the problem is how to determine how the user has left, and how to execute a file or a function when the user leaves.

After discussing some friends on the Internet, this problem finally solved.

The solution is: writing a general page, so-called general page, each page in the application contains this page, for example: header.asp, in this page, write a code with XMLHTTP, this code is A request is sent to the server every 10 seconds or 20 seconds to update the current user's online time and delete the online time of more than a certain period of time, so that the online user record in the database maintains a certain real-time.

The main implementation method is:

Create a new database, the field name is: ID (character), name, user (digital) TT (date), admin (permission code, 0-ordinary user, 1-administrator)

Table name: Online

HEADER.ASP

============================================================================================================================================================================================================= ==========

<%

...

IF session ("s_in") <> 1 and session ("s_name") = "" "" if the user is the first time

Rs.Open "Select * from online", conn, 3, 3 rs.addnew = session.SessionID RS ("Name") = "Visit" & session.SessionID RS ("User") = 0 '0 indicates that the user is not logged in, it is a tourist identity RS ("tt") = now rs.Update rs.close session ("s_in") = 1' Setting the user's information has been deposited into the database, indicating that the online endiff

If session ("s_name") <> "" "" "SELECT *" SELECT * from ONLINE WHERE ID = '"" & session.Session) or "" ") = session (" s_name ") = session (" s_admin ") 'Update the user's name is the member name RS (" User ") = 1' indicates that the user has logged in and is the member identity RS ("TT") = now 'Sets the current system time to the user's login time uUPDATE RS.CLOSEEND IF ...

%>

...

...