<%
set conn=server.CreateObject("ADODB.Connection")
conn.open "Driver={Microsoft Access Driver (*.mdb)};dbq="&server.MapPath("jishu.mdb")
'得到用户IP
IP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
if IP = "" then
IP = Request.ServerVariables("REMOTE_ADDR")
end if
notsql="select onlinename from online"
set notrs=conn.execute(notsql)
sql="select * from online where onlineIP='"& IP &"'" '判断此IP用户是否在线
set rs=server.CreateObject("ADODB.Recordset")
rs.open sql,conn,1,3
if rs.eof then '表示没有此IP用户不在线。
'设置区分在不在线的有效时间(5分钟)
uptotal="update tongjionline set totalonline=totalonline+1 where ID='1'"
conn.execute(uptotal)
effecttime=5
onlinetime=now()
ranNum=int(90000*rnd)+10000
onlinename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum
'给这个用户添加在线信息。
insql="insert into online(onlineIP,onlinename,lasttime) values('"& IP &"','"& onlinename &"','"& onlinetime &"')"
conn.execute(insql)
'对其他在线用户进行操作.
dels="select * from online"
set srs=server.CreateObject("ADODB.Recordset")
srs.open dels,conn,1,3
for n=0 to srs.recordcount
'response.Write srs("lasttime")&"<br>"
'删除有效时间内无任何操作的用户(似为离开)
if DateDiff("n",srs("lasttime"),now())>5 then
deluser=srs("onlinename")
delsql="delete from online where onlinename='"& deluser &"'"
conn.execute(delsql)
end if
srs.movenext
if srs.eof then exit for
next
'对其他在线用户的最近活动时间进行更新
upsql="update online set lasttime='"& onlinetime &"'"
conn.execute(upsql)
else '表示此IP用户已经在线
effecttime=2
onlinetime=now()
dels="select * from online"
set srs=server.CreateObject("ADODB.Recordset")
srs.open dels,conn,1,3
for n=0 to srs.recordcount
'删除有效时间内无任何操作的用户(似为离开)
'response.Write srs("lasttime")&"<br>"
if DateDiff("n",srs("lasttime"),now())>5 then
deluser=srs("onlinename")
delsql="delete from online where onlinename='"& deluser &"'"
conn.execute(delsql)
end if
srs.movenext
if srs.eof then exit for
next
'对其他在线用户的最近活动时间进行更新
upsql="update online set lasttime='"& onlinetime &"'"
conn.execute(upsql)
end if
'统计当前在线人数和历史最高在线人数.
num1sql="select count(onlinename) as nums from online"
set num1rs=conn.execute(num1sql)
nowonlinenum=cint(num1rs("nums")) '取得当前在线人数.
num2sql="select * from tongjionline"
set num2rs=conn.execute(num2sql)
num2rs.movefirst
if not num2rs.eof then
'如果当前在线人数大于最高统计在线人数则更新。
if cint(nowonlinenum)>cint(num2rs("maxonline")) then
maxdate=now() '取得新的最高在线人数发生时间。
nowonlinenum=cstr(nowonlinenum)
upnumsql="update tongjionline set maxonline='"& nowonlinenum &"',maxdate='"& maxdate &"' where ID='1'"
conn.execute(upnumsql)
end if
else
maxdate=now() '取得新的最高在线人数发生时间。
innumsql="insert into tongjionline(maxonline,maxdate) values('"& nowonlinenum &"','"& maxdate &"')"
conn.execute(innumsql)
end if
maxsql="select * from tongjionline where id='1'"
set maxrs=conn.execute(maxsql)
maxonline=maxrs("maxonline") '取得最高在线人数
maxdate=maxrs("maxdate") '取得最高在线人数发生时间。
'释放对象:
rs.close
set rs=nothing
notrs.close
set notts=nothing
srs.close
set srs=nothing
num1rs.close
set num1rs=nothing
num2rs.close
set num2rs=nothing
set totalrs=server.CreateObject("ADODB.Recordset")
totalsql="select * from tongjionline where ID='1'"
totalrs.open totalsql,conn,1,3
totalrs.movefirst
totalonline=totalrs("totalonline")
conn.close
%>
<table width="425" border="1">
<tr>
<td>总访问量:<%=totalonline%>====<%=uptotal%></td>
</tr>
</table>
数据表:
online:
字段 类型 说明
ID 自动编号 主键
onlineIP 文本 在线IP
onlinename 文本