利用fso统计在线人数
<%
function online()
'创建fso对象
dim fso
set fso = server.createobject("scripting.filesystemobject")
'定义纪录数据文件的路径和文件名
dim file
file = server.mappath("online.txt")
'检测文件是否存在,若不存在则新建
if not fso.fileexists(file) then
fso.createtextfile file,true,false
end if
'打开文件,向里面写入访客ip
dim ip,timenow
ip = request.ServerVariables("REMOTE_ADDR")
timenow = now()
if session(ip) = "" then
session(ip) = ip
dim txt
set txt = fso.opentextfile (file,8,false)
txt.writeline ip&"|"&timenow
txt.close
set txt = nothing
end if
'删除超时纪录
set txt = fso.opentextfile (file,1,false)
dim infostring,infoarray,i
do while not txt.atendofstream
infostring = txt.readline
infoarray = split(infostring,"|",-1,1)
if cdate(infoarray(1)) < now()-10/(24*60) then
i = i + 1
else
exit do
end if
loop
txt.close
set txt = nothing
set txt = fso.opentextfile (file,1,false)
dim a
for a = 1 to i
txt.skipline
next
dim onlineinfo
do while not txt.atendofstream
onlineinfo = txt.readall
loop
txt.close
set txt = nothing
set txt = fso.opentextfile (server.mappath("online_temp.txt"),2,true)
txt.write onlineinfo
txt.close
set txt = nothing
fso.deletefile file,true
fso.movefile server.mappath("online_temp.txt"),file
'读取文件中的数据,算出在线人数
set txt = fso.opentextfile (file,1,false)
dim onlinenum
onlinenum = 0
do while not txt.atendofstream
txt.readline
onlinenum = onlinenum + 1
loop
txt.close
set txt = nothing
'清空fso
set fso = nothing
online = onlinenum
end function
%>
当前在线<%=online()%>人