< %
sessionID = session.SessionID
timeout = 30
' 設(shè)置session的過期時間
Conn_String = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("count.mdb")
'建立數(shù)據(jù)庫連接項
Set ConnCount =Server.CreateObject("ADODB.Connection")
ConnCount.Open Conn_String
' dateadd函數(shù)的運(yùn)用,指定n分鐘為計量單位,計算now()+(-timeout)是時間,也就是過期時間
aaa = dateadd("n", -timeout, now())
connCount.Execute ("delete * from count where postdate < #" & aaa & "#")
'連接變量也可以用execute方法,對數(shù)據(jù)庫進(jìn)行操作,注意時間變量要用#隔開
' 以下語句是檢查客戶sessionID是否在數(shù)據(jù)庫中,如果不是就加入,客戶如果是第一次訪問那么數(shù)據(jù)庫中沒有值.也是用連接變量的execute方法,注意insert into count (sess,postdate) values('"&sessionID&"','"&now()&"')"運(yùn)用
sql0 = "select sess from count where sess='" & sessionID & "'"
set rscheck = connCount.Execute (sql0)
if rscheck.eof then
sql = "insert into count (sess,postdate) values('" & sessionID & "', '" & now() & "')"
connCount.Execute (sql)
end if
rscheck.close
set rscheck = nothing
'計算數(shù)據(jù)庫中的值,count(sess)產(chǎn)生新的值,最終用rs(0)讀出
sql2 = "select count(sess) from count"
set rs = connCount.Execute (sql2)
count = rs(0)
rs.close
set rs = nothing
'再對數(shù)據(jù)進(jìn)行全盤檢查看看是否有超期的數(shù)據(jù),有則數(shù)據(jù)count減一
sql3 = "select * from count"
set rspredel = connCount.Execute (sql3)
do until rspredel.eof
xxx=DateDiff("n", rspredel("postdate"), Now())
if xxx > timeout then
count = count-1
end if
rspredel.movenext
loop
rspredel.close
set rspredel = nothing
connCount.Close
set connCount = nothing
if count = 0 then
count = 1
end if
% >
document.write('當(dāng)前<%=count%>人在線');
'最終得出結(jié)果
'要計數(shù)的頁面中還要,插入<script language="javascript" src="XXXX/online.asp"></script>