1. Create a database table
Table Name: online
Set the following fields
Id' is used to record the session. sessionid of each access.
Name' if it is a visitor, it is recorded as a visitor.
Online, if the access is 0, if the access is 1
Datetime 'latest activity time
Username' indicates the username of the member. The guest is empty.
Ip address 'record logon IP addresses for all accesses
Head. asp: the number of online users written to the database table. This page must be searched into every ASP page used to browse.
<%
Set rs = Server. CreateObject ("ADODB. Recordset ")
If session ("username") = "" then determines that the user is not logged on
SQL = "select * from online where id = '" & session. sessionid & "'" 'determines whether the sessionid exists in the Database Table.
Rs. Open SQL, Conn, 1, 3
If rs. eof then' the visitor browsed for the first time
Rs. addnew
Rs ("id") = session. sessionID
Rs ("name") = "Tourists"
Rs ("online") = 0' 0 indicates that the user has not logged in. It is a tourist identity.
Rs ("datetime") = now ()
Userip = Request. ServerVariables ("HTTP_X_FORWARDED_FOR ")
If userip = "" Then
Userip = Request. ServerVariables ("REMOTE_ADDR ")
End if
Rs ("ip") = userip
Else 'guest not the first visit
Rs ("datetime") = now () 'update activity time
Rs. update
Rs. close
End if
Else
SQL = "select * from online where id = '" & session. sessionID & "'or admin ='" & session ("username") & "'"' determines that the sessionid or user name record already exists in the data table.
Rs. Open SQL, Conn, 1, 3
If rs. eof then
Rs. addnew' members log on to the website for the first time (they may log on to the forum directly from the homepage ).
Rs ("id") = session. sessionID
Rs ("name") = session ("show") 'write user nickname
Rs ("username") = session ("username") 'write the Login User Name
Rs ("online") = 1' indicates that the user has logged on and is a member.
Rs ("datetime") = now () 'sets the current system time to the user's logon time
Userip = Request. ServerVariables ("HTTP_X_FORWARDED_FOR ")
If userip = "" Then
Userip = Request. ServerVariables ("REMOTE_ADDR ")
End if
Rs ("ip") = userip
Else // the member does not browse the website for the first time and the visitor logs on to the website.
Rs ("name") = session ("show") update user nickname
Rs ("username") = session ("username ")
Rs ("online") = 1' indicates that the user has logged on and is a member.
Rs ("datetime") = now ()
End if
Rs. update
Rs. close
End if
Set rs = nothing
%>
Conn.exe cute ("delete from online where datediff ('s ', datetime, now ()> 60")' deletes a visitor who has no activity for 60 seconds. You can adjust the time by yourself.