先说一下实现的原理:
列表用户包括两种类型:来宾和系统用户。
当用户第一次登录系统时为他分配一个随机码(我是取他的SessionID),然后把这个随机码写入Cookies,并将过期时间设置的很久,三年了:)
然后在登录页面首先将来宾写入Application("online")变量,如果检测不到Cookies的值时,系统提示出错,无法继续进行下去,提示用户必须打开Cookies功能。
登录成功时,首先检查该用户是否已经存在于用户列表中,如果有而且承机码不同,那么可以判定是重复登录,给出提示,此用户已经登录,请在一定的时间后重新登录;如果没有用户就查找随机码,然后将相应的数组替换成用户数据。
用户退出有两种可能,正常退出和非正常退出。
正常退出很好处理,直接删除用户所在数组即可,如果非正常退出时将调用一个过程,此过程通过一个文件嵌套在所有页面中,当这些页面有任何用户刷新时都会检查用户列表,如果发现超时用户就删除该用户的列表数据。
我把以上的情况归纳成几个过程,这样可以很方便的调用。
Application("online")的格式为(用户名,登录时间,随机码,登录IP||)
当没有登录时用户名用“来宾”代替,如果有登录即用户变量来代替
以下是代码:
这段代码是放在首页的,即用户打开系统时的首页:
'设置网页不缓存
Response.Expires=-1
Response.AddHeader "Pragma","no-cache"
response.AddHeader "cache-control","no-cache"
'判断是否存在随机码Cookies,如果没有就写入SessionID为随机码,如果有就把它赋值给MMCode
If Request.Cookies("Code") = "" Then
MMCode = Session.SessionID
Response.Cookies("Code") = MMCode
Response.Cookies("Code").Expires = DateAdd("d",1000,Date)
Else
MMCode = Request.Cookies("Code")
End If
Application.Lock
'Application("online") = ""
If InStr(Application("online"),MMCode)=0 Then
Application("online") = Application("online")&"来宾,"&Now&","&MMCode&","&GetIP&"||"
End If
Online = Split(Application("online"),"||")'将Application("online")赋值给数组
For i = 0 to UBound(Online)-1'循环所有在线用户,如果存在随机码就更新登录时间为当前时间,
Online1 = Split(Online(i),",")
If Online1(2) = MMCode Then
OnlineTemp = Online1(0)&","&Now&","&Online1(2)&","&Online1(3)&"||"
Application("online") = Replace(Application("online"),Online(i)&"||",OnlineTemp)
End If
If DateDiff("n",Online1(1),Now)>15 Then'如果未活动时间超过15分钟就删除该用户的在线资料
Application("online") = Replace(Application("online"),Online(i)&"||","")
End If
Next
Application.Unlock
下面是相关的过程,放在登录页面,根据自己的需要放在合适的位置:
If Request.Cookies("Code") = "" Then
Response.Write "请打开IE的Cookies功能,系统需要此功能支持,如果你没有打开将不能使用本系统!"
Response.End
Else
MMCode = Request.Cookies("Code")
End If
Sub OnlineA()'更新资料在线客户资料
Application.Lock
'Application("online") = ""
If InStr(Application("online"),MMCode)=0 Then
Application("online") = Application("online")&"来宾,"&Now&","&MMCode&","&GetIP&"||"
End If
Online = Split(Application("online"),"||")'将Application("online")赋值给数组
For i = 0 to UBound(Online)-1'循环所有在线用户,如果存在随机码就更新登录时间为当前时间,
Online1 = Split(Online(i),",")
If Online1(2) = MMCode Then
OnlineTemp = Online1(0)&","&Now&","&Online1(2)&","&Online1(3)&"||"
Application("online") = Replace(Application("online"),Online(i)&"||",OnlineTemp)
End If
If DateDiff("n",Online1(1),Now)>15 Then'如果未活动时间超过15分钟就删除该用户的在线资料
Application("online") = Replace(Application("online"),Online(i)&"||","")
End If
Next
Application.Unlock
End Sub
Sub OnlineB()'退出登录时删除用户在线资料
Application.Lock
Online = Split(Application("online"),"||")'将Application("online")赋值给数组
For i = 0 to UBound(Online)-1'循环所有在线用户,如果存在随机码就更新登录时间为当前时间,
Online1 = Split(Online(i),",")
If (Online1(2) = MMCode) And (Online1(0) = Session("uName")) Then
Application("online") = Replace(Application("online"),Online(i)&"||","")
Exit For
End If
Next
Application.Unlock
End Sub
Sub OnlineC()'登录成功时更新用户在线资料
Application.Lock
Online = Split(Application("online"),"||")'将Application("online")赋值给数组
For i = 0 to UBound(Online)-1'循环所有在线用户,如果存在随机码就更新登录时间为当前时间,
Online1 = Split(Online(i),",")
If Online1(2) = MMCode Then
OnlineTemp = Session("uName")&","&Now&","&Online1(2)&","&GetIP&"||"
Application("online") = Replace(Application("online"),Online(i)&"||",OnlineTemp)
End If
Next
Application.Unlock
End Sub
Sub CheckOnline()'检测当前登录用户是否已经登录,如果已经登录则禁止重复登录。
Online = Split(Application("online"),"||")
For i = 0 to UBound(Online)-1
Online1 = Split(Online(i),",")
If Online1(2)<>MMCode And Online1(0)=Session("uName") Then
MsgStr = "该用户已经登录或者是你没有正常退出系统,请在5分钟后再试!"
Call Info1(StrTitle,MsgStr,ErrUrl)
End If
Next
End Sub
以下过程放在一个嵌套文件里,并且要调用最后一个函数一次:
Sub OnlineUser()'输出用户列表,可以根据需要自行修改
OnlineUserN = Split(Application("online"),"||")
For i = 0 to UBound(OnlineUserN)-1
OnlineUserN1 = Split(OnlineUserN(i),",")
Response.Write " "&OnlineUserN1(0)
Next
End Sub
Sub UpOnline()'更新资料在线用户资料
SessionCode = Request.Cookies("Code")
Application.Lock
Online = Split(Application("online"),"||")'将Application("online")赋值给数组
For i = 0 to UBound(Online)-1'循环所有在线用户,如果存在随机码就更新登录时间为当前时间,
Online1 = Split(Online(i),",")
If Online1(2) = SessionCode And Session("uName")=Online1(0) Then
OnlineTemp = Online1(0)&","&Now&","&Online1(2)&","&Online1(3)&"||"
Application("online") = Replace(Application("online"),Online(i)&"||",OnlineTemp)
ElseIf DateDiff("n",Online1(1),Now)>15 Then'如果未活动时间超过15分钟就删除该用户的在线资料
Application("online") = Replace(Application("online"),Online(i)&"||","")
End If
Next
Application.Unlock
End Sub
Call UpOnline()