表usr,用來(lái)存放注冊(cè)用戶(hù)的資料
字段id '用戶(hù)ID
字段name '用戶(hù)名
字段pass '密碼
表online,用來(lái)記錄用戶(hù)在線(xiàn)登陸情況,為了簡(jiǎn)單我只建立三個(gè)字段
字段oid '自動(dòng)編號(hào)
字段oname '用戶(hù)名
字段odata '記錄活動(dòng)時(shí)間(一開(kāi)始是登陸時(shí)間)
現(xiàn)在我們建立登陸頁(yè)login.asp
<%
if request("aciton")="chklog" then
dim rs,name,pass
name=trim(request.form("name"))
pass=trim(request.form("pass"))
if name="" or pass="" then
response.write("<script>alert('用戶(hù)名,密碼都不能為空!');history.go(-1)</script>"
response.end
else
'先查詢(xún)用戶(hù)名是否為注冊(cè)用戶(hù)
set rs=conn.execute("select * from usr where name='"&name&"'")
if rs.eof and rs.bof then '如果為空則提示錯(cuò)誤
rs.close:set rs=nothing '隨手關(guān)閉對(duì)象,釋放資源是個(gè)好習(xí)慣
response.write("<script>alert('沒(méi)有該注冊(cè)用戶(hù)!');history.go(-1)</script>"
response.end
else
if pass<>rs("pass") then '比較密碼
rs.close:set rs=nothing
response.write("<script>alert('密碼錯(cuò)誤!');history.go(-1)</script>"
response.end
else
rs.close:set rs=nothing
'用戶(hù)名,密碼都沒(méi)錯(cuò)后,查詢(xún)?cè)撚脩?hù)名是否已存在于online表中
set rs=conn.execute("select * from online where oname='"&name&"'")
if rs.eof and rs.bof then '如果為空,則說(shuō)明該用戶(hù)沒(méi)有登陸,插入該用戶(hù)名和當(dāng)前時(shí)間
rs.close:set rs=nothing
conn.execute"insert into online(oname,odata) values ('"&name&"',now())
session("username")=name
session("userpass")=pass
else '不為空說(shuō)明該用戶(hù)已登陸,給出提示
rs.close.set rs=nothing
response.write("<script>alert('該用戶(hù)已登陸,如果你非正常退出,請(qǐng)5分鐘后再?lài)L試登陸!');history.go(-1)</script>"
response.end
end if
end if
end if
response.redirect("index.asp") '轉(zhuǎn)到首頁(yè)
end if
end if
%>
下面html登陸代碼略過(guò)*******************************************************
接著我們來(lái)寫(xiě)退出頁(yè)面logout.asp
<% if session("username")<>"" and session("userpass")<>"" then '不為空,先刪除online表中的記錄
conn.execute"delete from online where oname='"&session("username")&"'"
end if
session("username")=""
session("userpass")=""
response.redirect("index.asp")
%>
下面開(kāi)始寫(xiě)最重要的文件了 online.asp
<%
dim rs,name
name=session("username")
if name<>"" then '不為空時(shí),則查詢(xún)online表是否存在該用戶(hù)名
set rs=conn.execute("select * from online where oname='"&name&"'")
if rs.eof and rs.bof then '為空時(shí)插入該用戶(hù)名記錄
rs.close:set rs=nothing
conn.execute"insert into online(oname,odata) values ('"&name&"',now())"
else '不為空時(shí),則更新該用戶(hù)活動(dòng)時(shí)間
rs.close:set rs=nothing
conn.execute"update online set odata=now() where oname='"&name&"'"
end if
end if
conn.execute"delete from online where datediff('s',odata,now())>300" '刪除300秒內(nèi)不活動(dòng)的用戶(hù)!
%>