[原创]ASP保持在线用户最新处理办法
- 开发笔记
- 2008-07-02
- 107热度
- 0评论
由于需要准确控制在线人数,但是很难捕获用户关闭浏览器的事件来清理online表中的在线用户记录,造成在线人数不准,利用global.asa的session_onEnd处理,又不能及时清除,只有在timeout和session被清空时才触发,没办法只能在数据表online加入Inser,Update触发器清除定时30分钟后的记录来保持在线人session超时后有人上线或更新online的date时间后删除超时的online记录。
OnLine表结构
[code]
****** 对象: Table [dbo].[online] 脚本日期: 07/02/2008 09:43:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create TABLE [dbo].[online](
[id] [int] IDENTITY(1,1) NOT NULL,
[compid] [int] NULL CONSTRAINT [DF_online_compid] DEFAULT ((0)),
[employeeId] [int] NULL,
[sdate] [datetime] NULL CONSTRAINT [DF_online_sdate] DEFAULT (getdate()),
[online] [int] NULL,
CONSTRAINT [PK_online] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
[/code]
页面每次进入或刷新时判断sdate是否超过30分钟,如果超过更新sdate的时间为当前最新时间,这样保持在线时间一直是最新的时间,避免被删除
[code]
'判断用户登录状态
if session("checklogin") then
if session("zjzxid")<>0 then
sessioncompid=session("zjzxid")
else
sessioncompid=session("compid")
end if
employee=session("employee")
set online=server.createobject("adodb.recordset")
'跟数据表中暂存的用户ID进行判断,其中sessioncompid中存储的是用户ID
online.open "select id,compid,sdate from online where compid="&sessioncompid&" and employeeid="&employee,conn,1,1
if online.recordcount<>0 then
'如果表中有该用户的话,就判断存储时间是否超出设置的超时时间
'如果超时,那么将系统时间赋上,以保证当前的用户的状态
oldid=online("id")
if DATEDIFF("n",online("sdate"),now())>30 then
sql="update online set sdate=getdate() where id="&oldid
conn.execute sql
end if
else
'如果表中没有该用户,则进行添加操作
sql="insert into online (compid) values ("&sessioncompid&")"
conn.execute sql
end if
online.close
set online=nothing
end if
[/code]
触发器
[code]
Alter TRIGGER [dbo].[clearOnline]
ON [dbo].[online]
AFTER Insert,Update
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with Select statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
Delete online where datediff(minute,sdate,getdate())>30
END
[/code]