统计在线网站人数.docx
《统计在线网站人数.docx》由会员分享,可在线阅读,更多相关《统计在线网站人数.docx(16页珍藏版)》请在冰豆网上搜索。
统计在线网站人数
1。
建立数据库表
表名为online
设如下字段
id'用来记录每一个访问都的session.sessionid
name'如果是访客,则记录为访客。
online'如果是访各为0如果是会员1
datetime'最近活动时间
username'会员的登录用户名,访客为空。
ip'记录访问都的登录IP
head.asp'向数据库表写入在线人数,该页面必须搜入到每一个用于浏览ASP页面中。
<%
setrs=Server.CreateObject("ADODB.Recordset")
ifsession("username")=""then判断用户未登录
sql="select*fromonlinewhereid='"&session.sessionid&"'"'判断这个sessionid是否存在于数据库表中.
rs.Opensql,Conn,1,3
ifrs.eofthen'访客第一次浏览
rs.addnew
rs("id")=session.sessionID
rs("name")="游客"
rs("online")=0'0表示用户未登陆,是游客身份
rs("datetime")=now()
userip=Request.ServerVariables("HTTP_X_FORWARDED_FOR")
Ifuserip=""Then
userip=Request.ServerVariables("REMOTE_ADDR")
endif
rs("ip")=userip
else'访客非第一次浏览
rs("datetime")=now()'更新活动时间
rs.update
rs.close
endif
else
sql="select*fromonlinewhereid='"&session.sessionID&"'oradmin='"&session("username")&"'"'判断sessionid或者用户名记录已存在数据表中。
rs.Opensql,Conn,1,3
ifrs.eofthen
rs.addnew'会员第一次进入网站(可能从网站首页直接登录进入论坛)。
rs("id")=session.sessionID
rs("name")=session("show")'写入用户昵称
rs("username")=session("username")'写入登录用户名
rs("online")=1'表示用户已经登陆,是会员身份
rs("datetime")=now()'将当前系统时间设置为用户的登陆时间
userip=Request.ServerVariables("HTTP_X_FORWARDED_FOR")
Ifuserip=""Then
userip=Request.ServerVariables("REMOTE_ADDR")
endif
rs("ip")=userip
else//会员非第一次浏览网站,访客登录网站。
rs("name")=session("show")更新用户昵称
rs("username")=session("username")
rs("online")=1'表示用户已经登陆,是会员身份
rs("datetime")=now()
endif
rs.update
rs.close
endif
setrs=nothing
%>
conn.execute("deletefromonlinewheredatediff('s',datetime,now())>60")'删除60秒没有活动的访客,时间可以自己调整。
本文来源:
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
以下介绍用数据库实现简单计数器,功能实现统计网站每日访问,每周访问及总访问量的统计,使用js调用
下面存为count.asp
<%
Setconn=Server.CreateObject("ADODB.Connection")
conn.Open"driver={MicrosoftAccessDriver(*.mdb)};dbq="&Server.MapPath("count.mdb")
%>
<%onerrorresumenext%>
<%sql="updatecountsethit=hit+1%><%conn.Execute(sql)%>
<%sql="select*fromcount
setrs=conn.execute(sql)
%>
更新每周每日数据
<%
lasthit=rs("lasthit")
tdate=year(Now())&"-"&month(Now())&"-"&day(Now())
iftrim(lasthit)=trim(tdate)then
sql="updatesitesetdayhit=dayhit+1whereid="&request("id")
conn.Execute(sql)
'response.write"success"
else
sql="updatesitesetdayhit=1whereid="&request("id")
conn.Execute(sql)
'response.write"error"
endif
sql="updatesitesetlasthit='"&tdate&"'whereID="&request("id")
conn.Execute(sql)
p_year=CInt(year(Now()))-CInt(year(lasthit))
p_month=CInt(month(Now()))-CInt(month(lasthit))
p_day=CInt(day(Now()))-CInt(day(lasthit))
period_time=((p_year*12+p_month)*30+p_day)
ifcint(period_time)=sql="updatesitesetweekhit=weekhit+1whereid="&request("id")
conn.Execute(sql)
else
sql="updatesitesetweekhit=1whereid="&request("id")
conn.Execute(sql)
endif
%>
document.write('
今日访问<%=rs("dayhit")%>次,本周访问<%=rs("weekhit")%>次,总访问<%=rs("hit")%>次
');
<%rs.close
setrs=nothing%>
用在要统计的页面调用即可.
建立数据库:
建一个count的MDB库,再建一个表count,表中字段为:
hit数字型
dayhit数字型
weekhit数字型
lasthit日期型
本文来源:
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
法一:
动态实现
<%
'OptionExplicit
'DimCountWeb
'CountWeb=Request.ServerVariables("SCRIPT_NAME")
Application.Lock
Application(CountWeb)=Application(CountWeb)+1
Application.UnLock
%>
欢迎光临!
您是第[<%=Application(CountWeb)%>]位客人。
法二:
javascript静态实现
--
varcaution=false
functionsetCookie(name,value,expires,path,domain,secure){
varcurCookie=name+"="+escape(value)+
((expires)?
";expires="+expires.toGMTString():
"")+
((path)?
";path="+path:
"")+
((domain)?
";domain="+domain:
"")+
((secure)?
";secure":
"")
if(!
caution||(name+"="+escape(value)).length<=4000)
document.cookie=curCookie
else
if(confirm("Cookieexceeds4KBandwillbecut!
"))
document.cookie=curCookie
}
functiongetCookie(name){
varprefix=name+"="
varcookieStartIndex=document.cookie.indexOf(prefix)
if(cookieStartIndex==-1)
returnnull
varcookieEndIndex=document.cookie.indexOf(";",cookieStartIndex+prefix.length)
if(cookieEndIndex==-1)
cookieEndIndex=document.cookie.length
returnunescape(document.cookie.substring(cookieStartIndex+prefix.length,cookieEndIndex))
}
functiondeleteCookie(name,path,domain){
if(getCookie(name)){
document.cookie=name+"="+
((path)?
";path="+path:
"")+
((domain)?
";domain="+domain:
"")+
";expires=Thu,01-Jan-7000:
00:
01GMT"
}
}
functionfixDate(date){
varbase=newDate(0)
varskew=base.getTime()
if(skew>0)
date.setTime(date.getTime()-skew)
}
varnow=newDate()
fixDate(now)
now.setTime(now.getTime()+365*24*60*60*1000)
varvisits=getCookie("counter")
if(!
visits)
visits=1
else
visits=parseInt(visits)+1
setCookie("counter",visits,now)
document.write("欢迎光临本站!
您是第"+visits+"客人")
//-->
本文来源:
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
今天才写的个,虽然简单了点,不过还是有用得着的地方。
<%
FunctionReadCount()
Application.Lock
DimCountNum,CountFile,fsoStr
fsoStr="Script"&"ing"&".File"&"System"&"Object"
CountFile="/count.txt"
CountNum=200380
Dimfso,f,tmpCount
OnErrorResumeNext
Setfso=Server.CreateObject(fsoStr)
Setf=fso.OpenTextFile(Server.MapPath(CountFile),1,True)
tmpCount=f.ReadAll
f.Close
IftmpCount=""OrNotIsNumeric(tmpCount)ThentmpCount=CountNum
IfIsEmpty(Session("Counter"))Then
tmpCount=tmpCount+1
Session("Counter")=tmpCount
Setf=fso.OpenTextFile(Server.MapPath(CountFile),2,False)
f.WritetmpCount
f.close
EndIF
Setf=Nothing
Setfso=Nothing
IfErr.Number<>0ThenErr.Clear
ReadCount=tmpCount
Application.Unlock
EndFunction
%>
下面的代码是最先写出来的,不过有问题,改成上面的就好了。
不是很懂
<%
FunctionReadCount()
Application.Lock
DimCountNum,CountFile,fsoStr
fsoStr="Script"&"ing"&".File"&"System"&"Object"
CountFile="/count.txt"
CountNum=200380
Dimfso,f,tmpCount
OnErrorResumeNext
Setfso=Server.CreateObject(fsoStr)
Setf=fso.OpenTextFile(Server.MapPath(CountFile),2,True)
tmpCount=f.ReadAll
IftmpCount=""OrNotIsNumeric(tmpCount)ThentmpCount=CountNum
IfIsEmpty(Session("Counter"))Then
tmpCount=tmpCount+1
Session("Counter")=tmpCount
f.WritetmpCount
EndIF
f.close
Setf=Nothing
Setfso=Nothing
IfErr.Number<>0ThenErr.Clear
ReadCount=tmpCount
Application.Unlock
EndFunction
%>
<%
'----------------访问统计改进-------------------------
'这个是因为听搞服务器的同事说上面的那个代码对服务器不是很好,因为会经常读写服务器,所以就改下面的了,如有更好的方案,大家交流交流!
FunctionReadCount()
Application.Lock
DimCountNum,CountFile,fsoStr
fsoStr="Script"&"ing"&".File"&"System"&"Object"
CountFile="/count.txt"'统计值保存文件
CountNum=200380'统计初始默认值
Dimfso,f,tmpCount
'获取统计数
OnErrorResumeNext
IfIsEmpty(Application("Counter"))OrApplication("Counter")=""OrNotIsNumeric(Application("Counter"))Then
Setfso=Server.CreateObject(fsoStr)
Setf=fso.OpenTextFile(Server.MapPath(CountFile),1,True)
tmpCount=f.ReadAll
f.close
Setf=Nothing
Setfso=Nothing
IftmpCount=""OrNotIsNumeric(tmpCount)ThentmpCount=CountNum
Application("Counter")=tmpCount
EndIF
'统计数自增Session("Counter")的作用是防止重复统计数自增
IfIsEmpty(Session("Counter"))Then
Application("Counter")=Application("Counter")+1
Session("Counter")=Application("Counter")
'如果统计数是100的倍数,把统计数写入到文件
IfApplication("Counter")Mod100=0Then
Setfso=Server.CreateObject(fsoStr)
Setf=fso.OpenTextFile(Server.MapPath(CountFile),2,True)
f.WriteApplication("Counter")
f.close
Setf=Nothing
Setfso=Nothing
EndIf
EndIF
IfErr.Number<>0ThenErr.Clear
ReadCount=Application("Counter")
Application.Unlock
EndFunction
%>
本文来源:
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
在目前的网站统计系统决大部分都是CGI的,但编写起来特别复杂,而ASP学起来简单,更有和数据库结合的优点,所以结合自己曾经做过的网站统计系统,和大家探讨一下ASP编写网站统计系统。
大家都看过网易的网站统计系统,它可以统计总访问量,每日平均访问量,当日访问量,最高访问量,最高访问日期,日流量分析,月流量分析,周流量分析,浏览器分析,等等。
其实要做一个ASP的访问统计系统关键是系统表结构的设计。
以及如何来采集用户的CGI变量,如何来显示用户的信息。
也就是说系统的关键是两个ASP程序,统计程序和显示程序。
首先我们先看看如何来采集用户的访问信息。
我们编写访问统计需要知道用户的如下信息,访问者的IP(根据访问IP可以形成访问者IP列表),访问者的浏览器与操作系统(统计访问者的浏览器与操作系统及所有访问者浏览器与操作系统比例图),访问者的访问时间(进行日访问量分析,月访问量分析,周访问量分析),下面我们来看看用ASP的获得以上信息的语句:
1。
获得访问者IP
DimM_IP
M_IP=Request.Servervariables('REMOTE_HOST')
用上面的语句可以取得访问者的IP。
2。
获得浏览器信息
DimO_Browser,M_BrowserType
SetO_Browser=Server.Createobject('MSWC.BrowserType')
M_BrowserType=O_Browser.Browser+O_Browser.Version
3。
获得访问时间
DimM_DateTime
M_DateTime=Year(Date())&'/'&Right('0'&Month(Date()),2)&'/'Right('0'&Day(Date()),2)&'/'&Right('0'&Hour(Time()),2)&':
'&Right('0'&Minute(Time()),2)&':
'&Right('0'&Second(Time()),2)
1 取得用户的操作系统。
在ASP中使用如下语句可以得到访问者的http_user_agent字符串。
DimStrUserAgent
StrUserAgent=Request.ServerVariables('HTTP_USER_AGENT')
这个字符串一般是如下格式:
Mozilla/4.0(compatible;MSIE4.01;Windows98)
上面的字符串可以说明访问者使用的操作系统是Windows98,浏览器是MSIE4.01,但是这个字符串格式不固定,而且可以自己更改。
平常我们所见的其它一些主要的UserAgent字符串如下:
使用IE的浏览器:
Mozilla/2.0(compatible;MSIE3.01;Windows95)
Mozilla/4.0(compatible;MSIE4.0;Windows95);
Mozilla/4.0(compatible;MSIE4.01;Windows98)
Mozilla/4.0(compatible;MSIE5.0;Windows98);
Mozilla/4.0(compatible;MSIE5.0b2;WindowsNT)
使用NetScape的浏览器:
Mozilla/4.03〔en〕(Win95;I)
Mozilla/4.08〔en〕(WinNT;U;Nav)
Mozilla/4.5〔en〕(WinNT;U)
Mozilla/3.04Gold(Win95;I)
使用Opera的浏览器:
Mozilla/4.0(compati