加入收藏 | 设为首页 | 会员中心 | 我要投稿 宁德站长网 (https://www.0593zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

一个简单的采用XML的在线人员统计类 - ASP教程

发布时间:2016-08-15 03:50:33 所属栏目:Asp教程 来源:站长网
导读:class online sub userState(uState) dim userIP,xmlDoc,root,newUser,delNode,oTime oTime=day(now())*24*60+hour(now())*60+minute(now()) set xmlDoc = serv
class online
sub userState(uState)
dim userIP,xmlDoc,root,newUser,delNode,oTime
oTime=day(now())*24*60+hour(now())*60+minute(now())
set xmlDoc = server.createObject("Msxml2.DOMDocument")
xmlDoc.async = false
xmlDoc.load(Server.mappath(xmlUrl & "online.xml"))
set root=xmlDoc.SelectNodes ("//online/at[@oSessionID=''" & session.sessionID & "'' or @oUname=''" & myCname & "'' or oUip=''" & userIP & "'']")
if root.length=0 then
set newUser=xmlDoc.createElement("at")
xmlDoc.documentElement.appendChild(newUser)
newUser.setAttribute "oSessionID",session.sessionID
newUser.setAttribute "oUname",myCname
newUser.setAttribute "oUtype",myUtype
newUser.setAttribute "oUstate",uState
newUser.setAttribute "oUip",userIP
newUser.setAttribute "oUactiveTime",oTime
set newUser=nothing
else
root.item(0).attributes.item(0).value=session.sessionID
root.item(0).attributes.item(1).value=myCname
root.item(0).attributes.item(2).value=myUtype
root.item(0).attributes.item(3).value=uState
root.item(0).attributes.item(4).value=userIP
root.item(0).attributes.item(5).value=now()
end if
set delNode=XmlDoc.documentElement.selectSingleNode("//online/at[@oUactiveTime<''" & oTime + 20 & "'']")
root.reomveChild(delNode)
set delNode=nothing
set root=nothing
xmlDoc.save(server.mappath(xmlUrl & "online.xml"))
end sub

sub onlineCount()
dim uOnline,gOnline,root,xmlDoc
set xmlDoc = server.createObject("Msxml2.DOMDocument")
xmlDoc.async = false
xmlDoc.load(Server.mappath(xmlUrl & "online.xml"))
set root=xmlDoc.SelectNodes ("//online/at[@oUtype=''1'']")
uOnline=root.length
set root=xmlDoc.SelectNodes ("//online/at[@oUtype=''0'']")
gOnline=root.length
set root=nothing
set xmlDoc=nothing
response.write "在线:" & Gonline+Uonline & " 会员:" & Uonline & " 游客:" & Gonline
end sub

function xChar(str)
xChar=str
xChar=replace(xChar,"""",""")
xChar=replace(xChar,"''","''")
xChar=replace(xChar,"<","<")
xChar=replace(xChar,">",">")
xChar=replace(xChar,"&","&")
end function
end class

虽然这段代码很简单但已函盖了XMLDOM的创建,保存,节点的增加,删除,节点的属性设置,DOM中利用XSL进行简单查询等等.

在在线人数比较多的站点,建议先用XSL进行索引,否则速度不堪想象.

文中oTime记录用户的最后活动时间,由于XML的日期期数据处理比较复杂,比如要用DTD,XSL等等给代码编写和用户使用造成不变,故这里将其转换为数字型.

而对在线的统计通过 选取的文档的 length 来取,当然你也可以直接用XSL的count函数.

xChar是XML特殊字符处理函数,这里没有加上字符处理,使用时在字符型数据处理时请使用此函数.当然如果你非常熟悉DTD等等语言,可以不用此函数而用它来对文档进行控制.

(编辑:宁德站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读