您的位置:首页 > 其它

用fso和session统计在线人数 文档管理系统 V2.0

2004-09-08 10:28 543 查看

用fso和session统计在线人数

文档管理系统 V2.0
Power By: Yanhang.00
作者:yanhang出处:Yanhang.00
编辑人:yanhang
rating:
全部文章——>>asp文档——>>代码实例返回
用fso和session统计在线人数

看了以前很多统计在线人数的文章,很多方法都是用global.asa文件,如果要是论坛的话则用数据库,但是有没有一种方法即不用global.asa文件,又不用数据库呢?当然有,这就是我今天要说的用fso和session统计在线人数。

总体思路是这样的:当用户访问该页面时,先给用户一个session,然后再向online.txt文本文件中写入一个信息,然后在删除超时用户的信息,这样就得出了当前在线人数的信息。我把这个功能编写成了一个函数,具体如下:

调用方法:online()

<%
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
%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: