完整的访问统计程序(二  程序篇)

/*
原创:豆腐
请大家在转载和使用的时候 保留这条信息
/
/
dbcn.asp
创建数据库连接的 公用程序
*/

 1   
 2function OpenConn()   
 3'创建数据库连接   
 4dim ConnStr   
 5dim uid   
 6dim pwd   
 7dim db   
 8uid="BBC"   
 9pwd="BBC"   
10db="BBC"   
11Softserver="Softdepserver"   
12Set Conn = Server.CreateObject("ADODB.Connection")   
13ConnStr="Provider=SQLOLEDB.1;UID=" & uid & ";PWD=" & pwd & ";database=" & db & ";server=" & Softserver & ""   
14  
15Conn.Open ConnStr   
16  
17set OpenConn=Conn   
18end function   
19  
20function OpenRst()   
21'创建数据库记录集   
22set Rst=Server.CreateObject ("ADODB.Recordset")   
23set OpenRst=Rst   
24end function   

/*
staticFunc
统计程序所需要用到的函数库
*/

1   
2set conn=openConn()   
3set rst=openRst()   
4sub WriteDateTable(intStart,intEnd,strArray,ccount)   
5'生成统计表   
1<table border="1" width="100%">   

for i= intStart to intEnd

1<tr>
2<td width="30%">```
3=strArray(i)
4```</td>
5<td width="60%">   

if rst.eof then
setbar 0,ccount
else
if rst(1)=i then
setbar rst(3),ccount
if not rst.eof then rst.movenext
else
setbar 0,ccount
end if
end if

1</td>
2</tr>   

next

1</table>
1   
2end sub   
1   
2sub setBar(num,ccount)   
3'生成统计条   
4if ccount=0 then   
5percent=0   
6else   
7percent=cInt(num/ccount*10000)/100   
8end if   
9strGif="bar.gif"   
1<img height="10" src="```
2=strGif
3```" width="```
4=percent+5
5```"/>
1<font color="red">```
2=percent
3```%</font>
1<td align="center">   

=num

1</td>
1   
2end sub   
 1   
 2sub writeDayStatic(userid)   
 3'生成日统计   
 4i=0   
 5dim strArray(100)   
 6for i=0 to 23   
 7strArray(i)= cStr(i) & "点--" & cstr(i+1) & "点"   
 8next   
 9i=0   
10set rst=openRst()   
11strSQL="select sum(num) as ccount from dateStatic where userid='" & userid & "'"   
12rst.open strSQL,conn,3,3   
13if isnull(Rst("ccount")) then   
14ccount=0   
15else   
16ccount=Rst("ccount")   
17end if   
18strSQL="select * from dateStatic where userid='" & userid & "' order by hourid"   
19set rst=openRst()   
20rst.open strSQL,conn,3,3   

访问总数``` =ccount

``` WriteDateTable 0, 11, strArray ,ccount `````` WriteDateTable 12, 23, strArray ,ccount ```
``` ```

end sub

sub writeWeekStatic(userid)
'生成周统计
i=0
dim strArray(7)
strArray(0)="星期日"
strArray(1)="星期一"
strArray(2)="星期二"
strArray(3)="星期三"
strArray(4)="星期四"
strArray(5)="星期五"
strArray(6)="星期六"
i=0
strSQL="select sum(num) as ccount from weekStatic where userid='" & userid & "'"
set rst=openRst()
rst.open strSQL,conn,3,3
if isnull(Rst("ccount")) then
ccount=0
else
ccount=Rst("ccount")
end if
strSQL="select * from WeekStatic where userid='" & userid & "' order by dayid"
set rst=openRst()
rst.open strSQL,conn,3,3

1访问总数```
2=ccount
1<table border="1" width="100%">
2<tr height="100%">
3<td width="100%">```
4 WriteDateTable 0, 6, strArray ,ccount 
5```</td>
6</tr>
7</table>
1   
2end sub   
 1   
 2sub writeMonthStatic(userid)   
 3'生成月统计   
 4i=0   
 5dim strArray(32)   
 6for i=1 to 31   
 7strArray(i)=cStr(i) & "日"   
 8next   
 9i=0   
10strSQL="select sum(num) as ccount from MonthStatic where userid='" & userid & "'"   
11set rst=openRst()   
12rst.open strSQL,conn,3,3   
13if isnull(Rst("ccount")) then   
14ccount=0   
15else   
16ccount=Rst("ccount")   
17end if   
18strSQL="select * from MonthStatic where userid='" & userid & "' order by dayid"   
19set rst=openRst()   
20rst.open strSQL,conn,3,3   

访问总数``` =ccount

``` WriteDateTable 1, 16, strArray ,ccount `````` WriteDateTable 17, 31, strArray ,ccount ```
``` ```

end sub

sub writeYearStatic(userid)
i=0
dim strArray(13)
for i=1 to 12
strArray(i)=cStr(i) & "月"
next
i=0
strSQL="select sum(num) as ccount from YearStatic where userid='" & userid & "'"
set rst=openRst()
rst.open strSQL,conn,3,3
if isnull(Rst("ccount")) then
ccount=0
else
ccount=Rst("ccount")
end if
strSQL="select * from YearStatic where userid='" & userid & "' order by monthid"
set rst=openRst()
rst.open strSQL,conn,3,3

1访问总数```
2=ccount
 1<table border="1" width="100%">
 2<tr height="100%">
 3<td width="50%">```
 4 WriteDateTable 1, 6, strArray ,ccount 
 5```</td>
 6<td width="50%">```
 7 WriteDateTable 7, 12, strArray ,ccount 
 8```</td>
 9</tr>
10</table>
1   
2end sub   
1   
2sub WriteRecentInfo(userid)   
3'显示访问者的浏览器分布   
4StrSQL="select top 10 * from accessstat where userid='" & userid & "' order by VisitTime desc"   
5set rst=openRst()   
6Rst.open StrSQL,Conn,3,1   
 1<table align="center" border="1" cellpadding="1" cellspacing="1" width="100%">
 2<tr>
 3<td align="center" width="5%">序 号</td>
 4<td align="center" width="20%">来访时间</td>
 5<td align="center" width="15%">IP地址</td>
 6<td align="center" width="15%">所用操作系统</td>
 7<td align="center" width="15%">所用浏览器类型</td>
 8<td align="center" width="30%">浏览的上一页</td>
 9<td width="0%"></td>
10</tr>   

for i=1 to Rst.RecordCount

 1<tr>
 2<td align="center" width="5%">```
 3=i
 4```</td>
 5<td align="center" width="20%">```
 6=Rst("VisitTime")
 7```</td>
 8<td align="center" width="15%">```
 9=Rst("IPAddr")
10```</td>
11<td align="center" width="15%">```
12=Rst("UseSystem")
13```</td>
14<td align="center" width="15%">```
15=Rst("BrowserType")
16```</td>
17<td align="center" width="30%"><a =rst("lastpage")="" ```="" href="```">```
18Response.Write Rst("LastPage")
19```</a></td>
20<td width="0%"></td>
21</tr>   

Rst.MoveNext

Next

1</table>
1   
2end sub   
1   
2sub WriteBrowserInfo(userid)   
3Set Rst=OpenRst()   
4strSQL="select count(*) as VisitCount from AccessStat where userid='" & userid & "'"   
5Rst.open strSQL,Conn,3,1   
6TotalVisit=rst(0)   
7StrSQL="select BrowserType,count(BrowserType) as visitcount from AccessStat where userid='" & userid & "' group by BrowserType order by VisitCount desc"   
8Set Rst=OpenRst()   
9Rst.open StrSQL,Conn,3,1   
1<table align="center" border="1" cellpadding="1" cellspacing="1" width="100%">
2<tr>
3<td align="center" width="5%">序 号</td>
4<td align="center" width="15%">所用浏览器类型</td>
5<td align="center" width="40%">访问比例尺表示</td>
6<td align="center" width="15%">访问次数</td>
7<td width="5%"></td>
8</tr>   

for i=1 to Rst.RecordCount

 1<tr>
 2<td align="center" width="5%">```
 3=i
 4```</td>
 5<td align="center" width="15%">```
 6=Rst("BrowserType")
 7```</td>
 8<!--   
 9<td width=15% align=center>```
10=Rst("VisitCount")
11```</td>   
12<td width=10% align=center>```
13=FormatNumber(CInt(Rst("VisitCount")/TotalVisit*10000)/100,true,true)
14```%</td>   
15<td width=40% align=left><img src=bar.gif width=```
16=(Rst("VisitCount")/TotalVisit)*300
17``` height=10></td>   
18\-->
19<td>```
20setbar Rst("VisitCount"),TotalVisit 
21```</td>
22<td width="5%"></td>
23</tr>   

Rst.MoveNext

Next

1</table>
1   
2end sub   
 1   
 2sub InsertData(userid)   
 3'处理浏览者信息   
 4ExecUserInfo userid   
 5'处理日统计   
 6ExecDayStatic userid   
 7'处理周统计   
 8ExecWeekStatic userid   
 9'处理月统计   
10ExecMonthStatic userid   
11'处理年统计   
12ExecYearStatic userid   
13end sub   
 1   
 2sub ExecUserInfo(userid)   
 3'处理浏览者信息   
 4strRet=Request.ServerVariables("HTTP_USER_AGENT")   
 5strArray=split(strRet,"(")   
 6strRet=left(strArray(1),len(strArray(1))-1)   
 7strArray=split(strRet,";")   
 8strBrowserName=strArray(1) '浏览器   
 9strUseSystem=strArray(2) '用户操作系统   
10strPrPage=Request.ServerVariables("HTTP_REFERER") '从何而来   
11if strPrPage="" then   
12strPrPage="http://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("SCRIPT_NAME")   
13end if   
14strIPAddr=Request.ServerVariables ("REMOTE_ADDR") '用户IP地址   
15strSQL="insert into AccessStat(UserID,VisitTime,IPAddr,UseSystem,BrowserType,LastPage) "   
16strSQL=strSQL & "values ('" & trim(userid) & "',getDate(),'" & trim(strIPAddr) & "','"   
17strSQL=strSQL & trim(strUseSystem) & "','" & trim(strBrowserName) & "','" & trim(strPrPage) & "')"   
18conn.execute strSQL   
19end sub   
 1   
 2sub ExecDayStatic(userid)   
 3'处理每日的小时数据   
 4strSQL="select * from DateStatic where dateid=datepart(dy,getdate()) and userid='" & userid & "'"   
 5set rst=openrst()   
 6rst.open strSQL,conn,3,3   
 7if rst.eof and rst.bof then   
 8strSQL="delete from DateStatic where userid='" & userid & "'"   
 9conn.execute strSQL   
10for i=0 to 23   
11strSQL="insert into DateStatic values(datepart(dy,getdate())," & cStr(i) & ",'" & userid & "',0)"   
12conn.execute strSQL   
13next   
14end if   
15strSQL="update datestatic set num=num + 1 where dateid=datepart(dy,getdate()) and hourid= datepart(hh,getdate()) and userid='" & userid & "'"   
16conn.execute strSQL   
17end sub   
 1   
 2sub ExecWeekStatic(userid)   
 3'处理每周的日数据   
 4strSQL="select * from WeekStatic where weekid=datepart(ww,getdate()) and userid='" & userid & "'"   
 5set rst=openRst()   
 6rst.open strSQL,conn,3,3   
 7if rst.eof and rst.bof then   
 8strSQL="delete from WeekStatic where userid='" & userid & "'"   
 9conn.execute strSQL   
10for i=0 to 6   
11strSQL="insert into WeekStatic values(datepart(ww,getdate())," & cStr(i) & ",'" & userid & "',0)"   
12conn.execute strSQL   
13next   
14end if   
15strSQL="update weekstatic set num=num + 1 where weekid=datepart(ww,getdate()) and dayid+1= datepart(dw,getdate()) and userid='" & userid & "'"   
16conn.execute strSQL   
17end sub   
 1   
 2sub ExecMonthStatic(userid)   
 3'处理每月的日数据   
 4strSQL="select * from MonthStatic where monthid=datepart(mm,getdate()) and userid='" & userid & "'"   
 5set rst=openRst()   
 6rst.open strSQL,conn,3,3   
 7if rst.eof and rst.bof then   
 8strSQL="delete from MonthStatic where userid='" & userid & "'"   
 9conn.execute strSQL   
10for i=1 to 31   
11strSQL="insert into MonthStatic values(datepart(mm,getdate())," & cStr(i) & ",'" & userid & "',0)"   
12conn.execute strSQL   
13next   
14end if   
15strSQL="update monthstatic set num=num + 1 where monthid=datepart(mm,getdate()) and dayid= datepart(dd,getdate())"   
16conn.execute strSQL   
17end sub   
 1   
 2sub ExecYearStatic(userid)   
 3'处理每年的月数据   
 4strSQL="select * from YearStatic where yearid=datepart(yy,getdate()) and userid='" & userid & "'"   
 5set rst=openRst()   
 6rst.open strSQL,conn,3,3   
 7if rst.eof and rst.bof then   
 8strSQL="delete from YearStatic where userid='" & userid & "'"   
 9conn.execute strSQL   
10for i=1 to 12   
11strSQL="insert into YearStatic values(datepart(yy,getdate())," & cStr(i) & ",'" & userid & "',0)"   
12conn.execute strSQL   
13next   
14end if   
15strSQL="update YearStatic set num=num + 1 where Yearid=datepart(yy,getdate()) and Monthid= datepart(mm,getdate()) and userid='" & userid & "'"   
16conn.execute strSQL   
17end sub   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus