1。建立数据库表
表名为online
设如下字段
id '用来记录每一个访问都的session.sessionid
name '如果是访客,则记录为访客。
online '如果是访各为0 如果是会员1
datetime '最近活动时间
username '会员的登录用户名,访客为空。
ip '记录访问都的登录IP
head.asp '向数据库表写入在线人数,该页面必须搜入到每一个用于浏览ASP页面中。
<%
set rs=Server.CreateObject("ADODB.Recordset")
if session("username")="" then 判断用户未登录
sql="select * from online where id='"&session.sessionid&"' " '判断这个sessionid是否存在于数据库表中.
rs.Open sql,Conn,1,3
if rs.eof then '访客{dy}次浏览
rs.addnew
rs("id")=session.sessionID
rs("name")="游客"
rs("online")=0 '0表示用户未登陆,是游客身份
rs("datetime")=now()
userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If userip = "" Then
userip= Request.ServerVariables("REMOTE_ADDR")
end if
rs("ip")=userip
else '访客非{dy}次浏览
rs("datetime")=now() '更新活动时间
rs.update
rs.close
end if
else
sql="select * from online where id='" & session.sessionID & "' or admin='"&session("username")&"'" '判断sessionid 或者 用户名记录已存在数据表中。
rs.Open sql,Conn,1,3
if rs.eof then
rs.addnew '会员{dy}次进入网站(可能从网站首页直接登录进入论坛)。
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")
If userip = "" Then
userip= Request.ServerVariables("REMOTE_ADDR")
end if
rs("ip")=userip
else //会员非{dy}次浏览网站,访客登录网站。
rs("name")=session("show") 更新用户昵称
rs("username")=session("username")
rs("online")=1 '表示用户已经登陆,是会员身份
rs("datetime")=now()
end if
rs.update
rs.close
end if
set rs=nothing
%>
conn.execute("delete from online where datediff('s',datetime,now())>60") '删除60秒没有活动的访客,时间可以自己调整。
本文来源:
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
以下介绍用数据库实现简单计数器,功能实现统计网站每日访问,每周访问及总访问量的统计,使用js调用
下面存为count.asp
<%
Set conn=Server.CreateObject("ADODB.Connection")
conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq="& Server.MapPath("count.mdb")
%>
<%on error resume next%>
<%sql="update count set hit=hit+1%><%conn.Execute(sql)%>
<%sql = "select * from count
set rs=conn.execute(sql)
%>
更新每周每日数据
<%
lasthit=rs("lasthit")
tdate=year(Now()) & "-" & month(Now()) & "-" & day(Now())
if trim(lasthit)=trim(tdate) then
sql="update site set dayhit=dayhit+1 where id="&request("id")
conn.Execute(sql)
' response.write "success"
else
sql="update site set dayhit=1 where id="&request("id")
conn.Execute(sql)
' response.write "error"
end if
sql="update site set lasthit='"&tdate&"' where ID="&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)
if cint(period_time)=<cint(7) then
sql="update site set weekhit=weekhit+1 where id="&request("id")
conn.Execute(sql)
else
sql="update site set weekhit=1 where id="&request("id")
conn.Execute(sql)
end if
%>
document.write('<tr><td width="{bfb}">今日访问<%=rs("dayhit")%>次,本周访问<%=rs("weekhit")%>次,总访问<%=rs("hit")%>次</td></tr>');
<%rs.close
set rs=nothing%>
用<script language="JavaScript1.1" src="count.asp"></script>在要统计的页面调用即可.
建立数据库:建一个count的MDB库,再建一个表count,表中字段为:
hit 数字型
dayhit 数字型
weekhit 数字型
lasthit 日期型
本文来源:http://hi.baidu.com/%D0%ED%EC%F7
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
法一:动态实现
<%
'Option Explicit
'Dim CountWeb
'CountWeb = Request.ServerVariables("SCRIPT_NAME")
Application.Lock
Application(CountWeb) = Application(CountWeb) + 1
Application.UnLock
%>
欢迎光临! 您是第[<font color="#FF0000"><%= Application(CountWeb)%></font>]位客人。
法二:javascript静态实现
<SCRIPT language=JavaScript>
<!--
var caution = false
function setCookie(name, value, expires, path, domain, secure) {
var curCookie = 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("Cookie exceeds 4KB and will be cut!"))
document.cookie = curCookie
}
function getCookie(name) {
var prefix = name + "="
var cookieStartIndex = document.cookie.indexOf(prefix)
if (cookieStartIndex == -1)
return null
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
if (cookieEndIndex == -1)
cookieEndIndex = document.cookie.length
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT"
}
}
function fixDate(date) {
var base = new Date(0)
var skew = base.getTime()
if (skew > 0)
date.setTime(date.getTime() - skew)
}
var now = new Date()
fixDate(now)
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000)
var visits = getCookie("counter")
if (!visits)
visits = 1
else
visits = parseInt(visits) + 1
setCookie("counter", visits, now)
document.write("欢迎光临本站!您是第" +visits + "客人")
// -->
</SCRIPT>
本文来源:
|
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
今天才写的个,虽然简单了点,不过还是有用得着的地方。
<%
Function ReadCount()
Application.Lock
Dim CountNum,CountFile,fsoStr
fsoStr="Script"&"ing"&".File"&"System"&"Object"
CountFile="/count.txt"
CountNum=200380
Dim fso,f,tmpCount
On Error Resume Next
Set fso=Server.CreateObject(fsoStr)
Set f = fso.OpenTextFile(Server.MapPath(CountFile),1,True)
tmpCount=f.ReadAll
f.Close
If tmpCount="" Or Not IsNumeric(tmpCount) Then tmpCount=CountNum
If IsEmpty(Session("Counter")) Then
tmpCount=tmpCount+1
Session("Counter")=tmpCount
Set f = fso.OpenTextFile(Server.MapPath(CountFile),2,False)
f.Write tmpCount
f.close
End IF
Set f = Nothing
Set fso = Nothing
If Err.Number <> 0 Then Err.Clear
ReadCount=tmpCount
Application.Unlock
End Function
%>
下面的代码是{zx0}写出来的,不过有问题,改成上面的就好了。不是很懂
<%
Function ReadCount()
Application.Lock
Dim CountNum,CountFile,fsoStr
fsoStr="Script"&"ing"&".File"&"System"&"Object"
CountFile="/count.txt"
CountNum=200380
Dim fso,f,tmpCount
On Error Resume Next
Set fso=Server.CreateObject(fsoStr)
Set f = fso.OpenTextFile(Server.MapPath(CountFile),2,True)
tmpCount=f.ReadAll
If tmpCount="" Or Not IsNumeric(tmpCount) Then tmpCount=CountNum
If IsEmpty(Session("Counter")) Then
tmpCount=tmpCount+1
Session("Counter")=tmpCount
f.Write tmpCount
End IF
f.close
Set f = Nothing
Set fso = Nothing
If Err.Number <> 0 Then Err.Clear
ReadCount=tmpCount
Application.Unlock
End Function
%>
<%
'----------------访问统计改进-------------------------
'这个是因为听搞服务器的同事说上面的那个代码对服务器不是很好,因为会经常读写服务器,所以就改下面的了,如有更好的方案,大家交流交流!
Function ReadCount()
Application.Lock
Dim CountNum,CountFile,fsoStr
fsoStr="Script"&"ing"&".File"&"System"&"Object"
CountFile="/count.txt"'统计值保存文件
CountNum=200380'统计初始默认值
Dim fso,f,tmpCount
'获取统计数
On Error Resume Next
If IsEmpty(Application("Counter")) Or Application("Counter")="" Or Not IsNumeric(Application("Counter")) Then
Set fso=Server.CreateObject(fsoStr)
Set f = fso.OpenTextFile(Server.MapPath(CountFile),1,True)
tmpCount=f.ReadAll
f.close
Set f = Nothing
Set fso = Nothing
If tmpCount="" Or Not IsNumeric(tmpCount) Then tmpCount=CountNum
Application("Counter")=tmpCount
End IF
'统计数自增 Session("Counter")的作用是防止重复统计数自增
If IsEmpty(Session("Counter")) Then
Application("Counter")=Application("Counter")+1
Session("Counter")=Application("Counter")
'如果统计数是100的倍数,把统计数写入到文件
If Application("Counter") Mod 100=0 Then
Set fso=Server.CreateObject(fsoStr)
Set f = fso.OpenTextFile(Server.MapPath(CountFile),2,True)
f.Write Application("Counter")
f.close
Set f = Nothing
Set fso = Nothing
End If
End IF
If Err.Number <> 0 Then Err.Clear
ReadCount=Application("Counter")
Application.Unlock
End Function
%>
本文来源:
|
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
在目前的网站统计系统决大部分都是CGI的,但编写起来特别复杂,而ASP学起来简单,更有和数据库结合的优点,所以结合自己曾经做过的网站统计系统,和大家探讨一下ASP编写网站统计系统。
大家都看过网易的网站统计系统,它可以统计总访问量,每日平均访问量,当日访问量,{zg}访问量,{zg}访问日期,日流量分析,月流量分析,周流量分析,浏览器分析,等等。
其实要做一个ASP的访问统计系统关键是系统表结构的设计。以及如何来采集用户的CGI变量,如何来显示用户的信息。也就是说系统的关键是两个ASP程序,统计程序和显示程序。
首先我们先看看如何来采集用户的访问信息。
我们编写访问统计需要知道用户的如下信息,访问者的IP(根据访问IP可以形成访问者IP列表),访问者的浏览器与操作系统(统计访问者的浏览器与操作系统及所有访问者浏览器与操作系统比例图),访问者的访问时间(进行日访问量分析,月访问量分析,周访问量分析),下面我们来看看用ASP的获得以上信息的语句:
1。获得访问者IP
Dim M_IP
M_IP=Request.Servervariables('REMOTE_HOST')
用上面的语句可以取得访问者的IP。
2。获得浏览器信息
Dim O_Browser,M_BrowserType
Set O_Browser=Server.Createobject('MSWC.BrowserType')
M_BrowserType=O_Browser.Browser+O_Browser.Version
3。获得访问时间
Dim M_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字符串。
Dim StrUserAgent
StrUserAgent=Request.ServerVariables('HTTP_USER_AGENT')
这个字符串一般是如下格式:
Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)
上面的字符串可以说明访问者使用的操作系统是Windows98,浏览器是MSIE 4.01,但是这个字符串格式不固定,而且可以自己更改。
平常我们所见的其它一些主要的UserAgent字符串如下:
使用IE的浏览器:
Mozilla/2.0 (compatible; MSIE 3.01; Windows 95)
Mozilla/4.0 (compatible; MSIE 4.0; Windows 95);
Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)
Mozilla/4.0 (compatible; MSIE 5.0; Windows 98);
Mozilla/4.0 (compatible; MSIE 5.0b2; Windows NT)
使用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 (compatible; Opera/3.0; Windows 95) 3.50b10
FrontPage编辑器:
Mozilla/2.0 (compatible; MS FrontPage 3.0)
使用Sun操作系统:
Mozilla/3.01Gold (X11; I; SunOS 5.7 i86pc)
使用PowerPc的Mac机:
Mozilla/4.0 (compatible; MSIE 4.5; Mac_PowerPC)
通过分析以上字符串,我们可以找出规律,编写一个子程序来判断访问者用的是何种操作系统,又由于用ASP中的判断浏览器类型的控件需要更新Browser.ini文件,所以我们可以结合这个字符串来判断浏览器属性。
2.我们以什么样的方式来统计网站呢?
我们可以让用户在他的主页上加入如下语句:
<a href='http://www.abc.com/viewer.asp?userid=username'><img src='http://www.abc.com/counter.asp?userid=username'></a>上面的userid是具体哪一个用户,要注意用户和访问者不是一个概念。
通过上面的字符串我们就可以采集用户的访问数据,并给用户提供观看数据的链接。在我们看加有网易的统计系统的页面时会发现它会返回给用户一个图标,我们就可以在counter.asp中实现这个功能。
加入:response.redirect 'http://www.abc.com/abc.gif'
这个语句我们可以加在统计数据采集后返回给用户。
设计表结构是一项极其重要的工作,它的合理与否与程序的编制紧密相关。
一个网站统计系统应该有一个用户表,统计值表。
这个用户表也就是保留注册用户信息的表,统计值表就是记录用户的每一项统计指标值的表。在统计值的表中我们可以指定用户的统计指标,我们可把每一个指标用一个ID值来表示,这里我们简单举例。
用户表:
Table Name:regist_table
Field type
username C 用户名
password C 密码
regdate C 注册时间
值表:
Table Name:value_table
Field type
username C 用户名
id C 统计指标ID
value C 统计指标的值
Id列表:
Table Name:id_table
Field type
id C 统计指标ID
idvalue C 统计指标的说明
有了这三个表我们就可以开始做了。
如我们可以指定以下ID
id idvalue
101 总访问量
201 1日访问量
202 2日访问量
: :
: :
231 31日访问量
要想对用户开始统计我们必须得先让用户注册,用户使用的流程如下:
填注册表->初始用户的value表(把对应的ID加入)->把注册信息反馈给用户
->用户在自己的页面上加入链接->开始统计
能采集数据我们都能采到了,那么我们就开始编制ASP的统计页面。
这一页我们叫计数页,counter.asp
这段asp代码我们需要让它按如下的流程进行采集数据并保存数据和更新数据:
采集用户名,判断用户名是否合法,采集访问者的信息,对信息进行加工,进行保存和更新数据库,返回logo图标。
调用这个ASP用counter.asp?user=abc这个方式。
采集用户名我们可以用Request对象的相应方法取得,然后检查用户表判断用户是否合法,然后取信息,用我们上面讲到的取得相应信息的方法取得信息,然后进行加工保存在数据表里,但最重要的是如何对数据进行更新,如当日每小时访问统计数,当天每小时统计数等,我们如何对每天的数据进行更新是这个程式的要点,我们可以采取几种方式,如对每天的每个小时的记录的更新我们采取在每天午夜12点更新,对每月的每{yt}的数据我们在月份切换的那{yt}进行更新。
本文来源:
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
目前的网站统计系统决大部分都是CGI的,但编写起来特别复杂,而ASP学起来简单,更有和数据库结合的优点,所以结合自己曾经做过的网站统计系统,和大家探讨一下ASP编写网站统计系统。
大家都看过中国站长站CNZZ的网站统计系统,它可以统计总访问量,每日平均访问量,当日访问量,{zg}访问量,{zg}访问日期,日流量分析,月流量分析,周流量分析,浏览器分析,等等。
其实要做一个ASP的访问统计系统关键是系统表结构的设计。以及如何来采集用户的CGI变量,如何来显示用户的信息。也就是说系统的关键是两个ASP程序,统计程序和显示程序。
首先我们先看看如何来采集用户的访问信息。
我们编写访问统计需要知道用户的如下信息,访问者的IP(根据访问IP可以形成访问者IP列表),访问者的浏览器与操作系统(统计访问者的浏览器与操作系统及所有访问者浏览器与操作系统比例图),访问者的访问时间(进行日访问量分析,月访问量分析,周访问量分析),下面我们来看看用ASP的获得以上信息的语句:
1、获得访问者IP
Dim M_IP
M_IP=Request.Servervariables("REMOTE_HOST")
用上面的语句可以取得访问者的IP。
2、获得浏览器信息
Dim O_Browser,M_BrowserType
Set O_Browser=Server.Createobject("MSWC.BrowserType")
M_BrowserType=O_Browser.Browser+O_Browser.Version
3、获得访问时间
Dim M_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)
4、取得用户的操作系统。
在ASP中使用如下语句可以得到访问者的http_user_agent字符串。
Dim StrUserAgent
StrUserAgent=Request.ServerVariables("HTTP_USER_AGENT")
这个字符串一般是如下格式:
Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)
上面的字符串可以说明访问者使用的操作系统是Windows98,浏览器是MSIE 4.01,但是这个字符串格式不固定,而且可以自己更改。
平常我们所见的其它一些主要的UserAgent字符串如下:
使用IE的浏览器:
Mozilla/2.0 (compatible; MSIE 3.01; Windows 95)
Mozilla/4.0 (compatible; MSIE 4.0; Windows 95)
Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)
Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)
Mozilla/4.0 (compatible; MSIE 5.0b2; Windows NT)
使用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 (compatible; Opera/3.0; Windows 95) 3.50b10
FrontPage编辑器:
Mozilla/2.0 (compatible; MS FrontPage 3.0)
使用Sun操作系统:
Mozilla/3.01Gold (X11; I; SunOS 5.7 i86pc)
使用PowerPc的Mac机:
Mozilla/4.0 (compatible; MSIE 4.5; Mac_PowerPC)
通过分析以上字符串,我们可以找出规律,编写一个子程序来判断访问者用的是何种操作系统,又由于用ASP中的判断浏览器类型的控件需要更新Browser.ini文件,所以我们可以结合这个字符串来判断浏览器属性。
1.我们以什么样的方式来统计网站呢?
我们可以让用户在他的主页上加入如下语句:
<a href="">
<img src=""></a>
上面的userid是具体哪一个用户,要注意用户和访问者不是一个概念。
通过上面的字符串我们就可以采集用户的访问数据,并给用户提供观看数据的链接。在我们看加有网易的统计系统的页面时会发现它会返回给用户一个图标,我们就可以在counter.asp中实现这个功能。
下面是我自己写的代码:数据库,很简单:两个字段,num和dat
<%
set con=server.CreateObject("ADODB.Connection")
str="provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("counter.mdb")
con.open str
set rs=server.CreateObject("ADODB.RecordSet")
sql="select * from table1 where dat='"&cstr(date())&"'"
rs.open sql,con,1,1
'response.Write(session("tag"))
'session("tag")=empty
if session("tag")=empty then
if rs.eof then
con.execute("insert into table1(dat) values('"&cstr(date())&"')")
con.execute("update table1 set numb=numb+1 where dat='"&cstr(date())&"'")
else
sql="update table1 set numb=numb+1 where dat='"&cstr(date())&"'"
con.execute(sql)
end if
session("tag")="1"
end if
'response.Write(session("tag"))
'esponse.End()
set rs=server.CreateObject("ADODB.recordset")
sql="select * from table1 order by dat desc"
rs.open sql,con,1,1
today=rs("numb")
sum=sum+rs("numb")
if max<rs("numb") then
max=rs("numb")
end if
rs.movenext
yerstoday=rs("numb")
sum=sum+rs("numb")
if max<rs("numb") then
max=rs("numb")
end if
rs.movenext
while not rs.eof
sum=sum+rs("numb")
if max<rs("numb") then
max=rs("numb")
end if
rs.movenext
wend
response.write("今日访问量:"&today&" ")
response.Write("昨日访问量:"&yerstoday&" ")
response.Write("总访问量:"&sum)
response.Write("{zg}访问量:"&max)
%>
</body>
</html>
上面的那些不知道运行得了波?参考吧,我写的是可以运行的。