======Production Principle======
The method is to add the user's information into the database when the user accesses the webpage. While adding, check whether there is an online record of the user in the database. If so, update it. If the record does not exist, add it to the database.
And delete online records that have no activity within the specified time. (That's probably it!)
======Data table design========
Create a new data table named "Online"
Delete the automatic number field and create the following fields. Field name: ID Type: Number Field name: GUESTNAME Type: Text field name: STATS Type: Text field name: VISITIME Type: Date/Time Field name: OUTIME Type: Date/Time
=======================The following part of the source code is for reference. If it is not well written, please correct me============ ===========
<%
sub activeonline()
dim ip
'////Delete online records that are inactive within 180 seconds.
sql="Delete FROM online WHERE DATEDIFF('s',outtime,now())>180"
Conn.Execute sql
if stats="" then'//If the value of stats is empty, it is displayed as
stats="Don't know what you are doing?"
else
stats=stats
end if
IP=replace(Request.ServerVariables("REMOTE_HOST"),".","")'////Get the IP and remove the "." in
the IP '////Check whether this already exists in the Online table IP record
sql="select id from online where id='"&ip&"'"
set rs=conn.execute(sql)
if rs.eof or rs.bof then'////If there is no IP record, add an online record
sql="insert into online(id,guestname,stats,visitime,outtime) values ("&ip&",'Guest','"&stats&"',Now(),Now())"
else'////If the IP record already exists in the Online table, update the record
sql="update online set outime =Now(),stats='"&stats&"',guestname='guest' where id='"&ip&"'"
end if
conn.execute(sql)
end sub
%>
=========================Examples======================== ====
Modify the above code and save it as "Online.asp" and embed it at the end of each web page
<%
dim conn
dim connstr
on error resume next
connstr="DBQ="+server.mappath("database name.mdb")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
set conn=server.createobject("ADODB.CONNECTION")
conn.open connstr
'Save as conn.asp file
%>
<!--#INCLUDE FILE="conn.asp" -->
<%
dim stats
stats="View online"
call activeonline()
Set rs = Server.CreateObject("ADODB.Recordset")
sql="SELECT Id,GuestName,Stats,Visitime,Outime FROM Online ORDER BY Visitime Desc"
rs.open sql,conn,1,3
total=rs.RecordCount
%>
<table border="1" cellpadding="2" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" height="53">
<tr>
<td width="20%" height="16" align="center">Nickname</td>
<td width="20%" height="16" align="center">Action</td>
<td width="20%" height="16" align="center">Visit</td>
<td width="20%" height="16" align="center">Last event</td>
</tr>
<%do while not rs.eof%>
<tr>
<td width="20%" height="28" align="center"><%=rs(1)%></td>
<td width="20%" height="28" align="center"><%=rs(2)%></td>
<td width="20%" height="28" align="center"><%=rs(3)%></td>
<td width="20%" height="28" align="center"><%=rs(4)%></td>
</tr>
<%
rs.movenext
loop
%>
</table>
Number of people online:<%=total%>
<%
rs.close
set rs=nothing
%><!--#INCLUDE FILE="Online.asp" -->
It's not written well, it's ridiculous. If you have a better method, please submit it. Let's learn from each other!