'检索数据库中是否存在相关记录
strSQL = "Select * From MEMBER Where NAME='" & UserName & "'"
strSQL = strSQL & " And PASSWORD = '" & UserPass & "'"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open strSQL, objConn, 1, 3, 1
'检索结果为空,表明用户名或者密码错误
If objRS.EOF Then
Response.Redirect "login.asp?ErrMsg=用户名或密码错误"
Response.End()
Else
'检索记录集不为空,说明用户名和密码输入正确
Session("Id") = CStr(objRS.Fields("ID"))
Session("Name") = UserName
Session("Class") = objRS.Fields("CLASS")
Session("IsPassed")=TRUE
'检查用户的级别
If objRS.Fields("CLASS") = "1" OR objRS.Fields("CLASS") = "2" Then
'该登陆用户为管理员
Session("IsAdmin") = TRUE
Else
'该登陆用户为普通用户
Session("IsAdmin") = FALSE
End If
'如果用户选择记住帐号和密码,则将其写入cookies
If Request.Form("RemMe") = "1" Then
Response.Cookies("Name") = UserName
Response.Cookies("Password") = UserPass
Response.Cookies("RemMe") = "1"
Response.Cookies("Name").expires = Date + 365
Response.Cookies("Password").expires = Date + 365
Response.Cookies("RemMe").expires = Date + 365
End If
End If
'关闭记录集和数据库连接
objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
'检查用户是否已经登陆,为真则直接进入系统主界面
If Session("IsPassed") = EMPTY Or Session("IsPassed") = FALSE Then
'检查Cookies,是否有保存的用户信息
If Request.Cookies("RemMe") ="1" Then
'存在Cookies信息,自动进行登陆操作
UserName = Request.Cookies("Name")
UserPass = Request.Cookies("Password")
strSQL = "Select * From MEMBER Where NAME='" & UserName & "'"
strSQL = strSQL & " And PASSWORD = '" & UserPass & "'"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open strSQL, objConn, 1, 3, 1
'是否存在这个用户
If NOT objRS.EOF Then
Session("Id") = CStr(objRS.Fields("ID"))
Session("Name")=UserName
Session("IsPassed")=TRUE
Session("Class") = objRS.Fields("CLASS")
'检索记录集不为空,说明用户名和密码输入正确
'检查用户的级别
If objRS.Fields("CLASS") = "1" OR objRS.Fields("CLASS") = "2" Then
'该登陆用户为管理员
Session("IsAdmin") = TRUE
Else
'该登陆用户为普通用户
Session("IsAdmin") = FALSE
End If
'更新Cookies
Response.Cookies("Name") = UserName
Response.Cookies("Password") = UserPass
Response.Cookies("RemMe") = "1"
Response.Cookies("Name").expires = Date + 365
Response.Cookies("Password").expires = Date + 365
Response.Cookies("RemMe").expires = Date + 365
Else
'Cookies中的用户名或密码错误,返回login.asp要求重新登陆
Response.Redirect "login.asp?ErrMsg=用户名或密码错误"
Response.End
End If