ASP统计用户在线时间

来源:互联网 发布:淘宝盗图投诉 编辑:程序博客网 时间:2024/04/29 04:08

如果你自己对ASP还算熟悉的话,我可以给你讲一个不用Ajax的思路,但是考虑的服务器的负载,统计的精度不能太高,不然会增加服务器的负载:

在你在数据库里用一个叫time的字段来存储累计时间;
在用户登录成功后的第一个页面定义一个session("time1")=now()来表示用户的登录时间;

新建一个页面refresh.asp,这个页面是用来实时上传当前时间的,页面内容为:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#Include File="../Conn.asp"-->
<%
dim timers,timestr,time_now
time_now=now()
timestr="select * from [你的表] where username='"&session("username")&"'"

set timers=server.CreateObject("adodb.recordset")
timers.open timestr,kaConn,1,3
timers("time")=timers("time")+datediff("s",session("time1"),time_now)
'累计时间timers("time")=原有时间+(当前时间 - 初始时间),datediff()是一个计算时间差值的函数,第一个参数是设置差值的单位,"s"表示差值单位为秒,第二个参数是时间1,第三个参数是时间2,差值为时间2减时间1;
timers.update
timers.close
set timers=nothing
kaConn.close
set kaConn=nothing
session("time1")=now() '从新设置初始时间为当前时间,为下一次计算作准备
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="refresh" content="60"> (这里是设置页面刷新时间间隔的,也就是确定你的统计精度,这里60的意思是60秒钟刷新一次,也就是上传一次数据)
<title>refresh</title>
</head>

<body>
</body>
</html>


上面的refresh.asp不是单独使用的而是作为一个框架窗口嵌入到用户登录后的所有需要统计时间的页面中,不过你可以设置这个框架的大小为0x0,这样用户是看不到这窗口的,但是这个隐藏的refresh.asp页面却在不段的更新着用户的累计在线时间。因为这个隐藏页面每60秒就刷新一次,就会执行上面的程序来更新数据库,但是主页面是看不到刷新的。
下面这个是用来插入隐藏页面的代码,写在需要统计时间的页面的底部吧
<iframe src="refresh.asp" name="refresh" width="0" height="0" allowtransparency="true" scrolling="no" frameborder="0">

原创粉丝点击