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

用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
%>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值