< % sessionID = session.SessionID timeout = 30 ' 设置session的过期时间 Conn_String = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("count.mdb") '建立数据库连接项 Set ConnCount =Server.CreateObject("ADODB.Connection") ConnCount.Open Conn_String
' dateadd函数的运用,指定n分钟为计量单位,计算now()+(-timeout)是时间,也就是过期时间 aaa = dateadd("n", -timeout, now()) connCount.Execute ("delete * from count where postdate < #" & aaa & "#") '连接变量也可以用execute方法,对数据库进行操作,注意时间变量要用#隔开
' 以下语句是检查客户sessionID是否在数据库中,如果不是就加入,客户如果是第一次访问那么数据库中没有值.也是用连接变量的execute方法,注意insert into count (sess,postdate) values('"&sessionID&"','"&now()&"')"运用 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
'计算数据库中的值,count(sess)产生新的值,最终用rs(0)读出 sql2 = "select count(sess) from count" set rs = connCount.Execute (sql2) count = rs(0) rs.close set rs = nothing
'再对数据进行全盘检查看看是否有超期的数据,有则数据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('当前<%=count%>人在线'); '最终得出结果 '要计数的页面中还要,插入<script language="javascript" src="XXXX/online.asp"></script>
|