用户表里有一IsLogin字段,当用户登录成功后,将IsLogin设为TRUE。
退出登录时,将将IsLogin设为FALSE。
想通过查询IsLogin为TRUE的用户数量,而显示出登录用户的人数(即在线注册用户的人数)
问题:但是如用户登录后,并没有执行退出登录功能而直接关闭浏览器的话,UserLogOUT()并没有被执行,即用户表中的IsLogin字段一直为TRUE,这怎么办?
============
登录按扭代码:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim CurrentUser As UserInfo
'用Session("CurrentUser")来实例化CurrentUser,此时CurrentUser.Islogin=False
CurrentUser = DirectCast(Session("CurrentUser"), UserInfo)
'将用户输入的用户名和密码赋给CurrentUser.UserName,CurrentUser.UserPWD属性,准备验证
CurrentUser.UserName = TextBox1.Text
CurrentUser.UserPWD = TextBox2.Text
'通过 Userlogin()子程序来验证能否登录,能登录,则CurrentUser.Islogin=TRUE,否则为FALSE(具体请见Userlogin()子程序)
CurrentUser = Userlogin(CurrentUser)
End Sub
退出登录按钮的代码:
Private Sub btnLogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim CurrentUser As UserInfo
'用Session("CurrentUser")来实例化CurrentUser
CurrentUser = DirectCast(Session("CurrentUser"), UserInfo)
CurrentUser = UserlogOut(CurrentUser)
End Sub
登录验证子程序的代码如下:
Public Function Userlogin(ByVal MyUserInfo As UserInfo) As UserInfo
Dim CurrentUser As New UserInfo
Dim mySql As String
mySql = "Select UserID from Employee where UserName='" & MyUserInfo.UserName & "'"
mySql &= " and UserPWD='" & MyUserInfo.UserPWD & "'"
ObjSql.strSQL = mySql
Dim dr As SqlDataReader = ObjSql.SelectFromDBrd()
If dr.Read Then
MyUserInfo.IsLogin = True
MyUserInfo.UserId = dr("UserId")
dr.Close()
'将用户表中的IsLogin字段改为TRUE(1)(*****重点看看这句*****)
mySql = "Update Employee set IsLogin=1 where userid='" & MyUserInfo.UserId & "'"
ObjSql.strSQL = mySql
ObjSql.ExecuteSQL()
Else
'用户不能登录。。。。
MyUserInfo.IsLogin = False
dr.Close()
End If
CurrentUser = MyUserInfo
Return CurrentUser
end sub
退出登录子程序的代码如下:
Public Function UserlogOut(ByVal MyUserInfo As UserInfo) As UserInfo
Dim CurrentUser As New UserInfo
Dim mySql As String
If MyUserInfo.IsLogin = True Then
'将用户表中的IsLogin字段改为FALSE(0)(*****重点看看这句*****)
mySql = "Update Employee set IsLogin=0 where userid='" & MyUserInfo.UserId & "'"
ObjSql.strSQL = mySql
ObjSql.ExecuteSQL()
MyUserInfo.IsLogin = False
End If
CurrentUser = MyUserInfo
Return CurrentUser
end sub
这两个子程序均能正常工作。
问题:但是如用户登录后,并没有执行退出登录功能而直接关闭浏览器的话,UserLogOUT()并没有被执行,即用户表中的IsLogin字段一直为TRUE,这怎么办?
将UserLogOUT()放在Session On end 中也不行:
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' SESSION开始时,建立一个Session("CurrentUser")
Dim CurrentUser As New UserInfo
Session("CurrentUser") = CurrentUser
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' 在会话结束时激发
UserlogOut(DirectCast(Session("CurrentUser"), UserInfo))
End Sub