当前位置:主页   - 电脑 - 网站开发 - ASP
网站统计代码-只要包含此代码即可统计
来源:网上收集   作者:未知   更新时间:2008-12-07
收藏此页】    【字号    】    【打印】    【关闭
Have you ever tried to use one of the many fancy, hightech, multifeatured, superfast and custom made web
server statistics? I do not know about you, but I did. Every time I looked that 20+ page report, I was
pretty sure that somewhere between thousands of lines, there must be simple answer to my questions: How
many times was my page downloaded? After few tries I gave up and wrote my own, self administrated,
blindingly obvious server statistics.

Idea behind this code is very simple, in order to know what is interesting to visitors of my web site I
have implemented this simple counter to each page which was interesting to me. If you examine your site,
you can easily find that there are two different types of .ASP pages; most of them usually have only some
basic code for example; session variable with background name, call to lastmodified component, or reading
data from database) while other are used for redirecting, storing data into databases, trigering mail
components etc. Well, my counter was suposed to be present on all pages which have some user viewable
content. All you have to do is just to include file which contains counter code (whenever possible use
include files, half of the strength of ASP lies in power include files):

Code snippet nr. 1

1 <!--#include virtual="/marko/last001.asp" -->




Include file itself is very simple and it contains only 30 lines. Basically, what I am doing is opening
database connection (lines 1 to 4). In line 5 I am looking what is exact URL and I am translating
everything into lower case(remember to put everything into lower case, because someone can have CAPS LOCK
TURNED ON). After that (lines 7 to 9), I am trying to find this URL in my counter database. If there is no
record with this exact URL (lines 11 to 16), I am assuming that this is new page and I am inserting new
records in database and setting counters to one (well, this is obviously first time that this page is
viewed). In the case there is record with given URL (line 18) I am simply increasing counter values by one
(lines 19 to 23). After that, take it easy, close your connections and destroy used objects (lines 25 to
30). Voila!

Code snippet nr. 2 - file counter.asp

1 <% Set count = Server.CreateObject("ADODB.Connection")
2 count.Open "NeT"
3
4 set countrs = Server.CreateObject("ADODB.RecordSet")
5 url = LCase(Request.ServerVariables("URL"))
6
7 sqlstr = "SELECT TOP 1 * FROM counter WHERE url='" & url & "' ORDER BY url"
8
9 countrs.Open sqlstr, count, 3 , 3, 1
10
11 if countrs.recordcount < 1 then
12 sqlstr= "INSERT INTO counter "
13 sqlstr= sqlstr & "(url, total, today, month, last, yesterday ) VALUES "
14 sqlstr= sqlstr & "('" & url & "', 1, 1, 1, 0, 0)"
15
16 count.Execute(sqlstr)
17
18 ElseIf countrs.recordcount > 0 then
19 countrs.Fields("today") = CInt(countrs.Fields("today")) + 1
20 countrs.Fields("month") = CInt(countrs.Fields("month")) + 1
21 countrs.Fields("total") = CLng(countrs.Fields("total")) + 1
22
23 countrs.Update
24 End If
25
26 countrs.Close
27 count.Close
28
29 set countrs = Nothing
30 set count = Nothing %>




There is also another file called report.asp which will simply render data from your database in one
table. File is really simple and it renders results from your database into table. First I am creating a
table (lines 1 to 13), opening database connection and selecting all records (lines 15 to 17), setting
total counter and this month counter to 0 (lines 19 and 20), and starting do while loop until I hit end of
file (line 22). In the table there are three columns, one with URL (see line 26), second with total number
of hits (line 30) and third with total number of hits this month (line 34). Note that I am also increasing
values for total counters for each column (lines 30 and 34). After I exit my loop I am closing database
connections and destroying used objects (lines 41 to 45). If you check values in variables "total"
and "month" you will get total number page loading and total number of page loading in current month.


Code snippet nr. 3 - file report.asp

1 <table border="1" cellpadding="3" cellspacing="0" width="80%" align="center">
2
3 <tr>
4 <td>
5 <font size="1" face="Arial"><center>URL</font>
6 </td>
7 <td>
8 <font size="1" face="Arial"><center>Total count</font>
9 </td>
10 <td>
11 <font size="1" face="Arial"><center>This month</font>
12 </td>
13 </tr>
14
15 <% Set Conn = Server.CreateObject("ADODB.Connection")
16 Conn.Open("net")
17 Set RS = Conn.Execute("SELECT * FROM counter where month > 0 ORDER BY month DESC")
18
19 total = 0
20 month = 0
21
22 Do While RS.EOF %>
23
24 <tr>
25 <td width="80%">
26 <a href="<%= RS("url") %>"><%= RS("url") %></a></font>
27 </td>
28 <td align="right">
29 <font size="2">
30 <%= RS("total") %><% total = total + CLng(RS("total")) %>
31 </td>
32 <td align="right">
33 <font size="2">
34 <b><%= RS("month") %><% month = month + CInt(RS("month")) %></b>
35 </td>
36 </tr>
37
38 <% RS.MoveNext
39 Loop
40
41 RS.Close
42 Conn.Close
43
44 set rs = nothing
45 set conn = nothing %>
46
47
48 </table>




Web statistics is neatly packed into this stat.zip archive, but remember that there are some items missing
here; You have to create your own table and database connection. In my example, I am using database with
following records:

Code snippet nr. 4 - database structure

1 Database counter.dbf structure
2
3 url, text field, at least 60 characters
4 total, long integer
5 today, long integer
6 month, long integer
7 last, long integer
8 yesterday, long integer




URL field contains exact page URL, total counter contain total number of hits, today contain daily number
of hits, month is current month counter, last contains last months results and yesterday contains
yesterday number of hits. Note that I use long integers, your site with just few hundreds visitors per day
will cross integer limits very far and there is posibility that you will wonder what happened wrong for a
long time before discovering that you just hit integer roof.Some of this fields can be omited, but with
this configuration you can be pretty sure to know what is going on on your site. Of course, in order to
all counters work right, you need to establish routine which will regulary (at midnight each day, and at
midnight on the first day in the month) move data into apropriate collumns (today into yesterday and this
month into last month collumn). I have done this by creating "autoexec.asp" file which is called each time
new user enters web site, I check if date is changed since last user, and if it is, I perform
database "house cleaning". Since there are just few pages in database (everything up to few hundred pages
can be called "few pages"), user waiting time is close to or equal to zero.
Please, do not forget to index at least URL field in order to maximize performance. If your site have
significant traffic, please do not use Access database because it is a real turtle, use any other database
instead (I use Visual FoxPro which significantly narrows circle of people who may help me when I hit a
problem, but it is pretty fast, and it is somewhere in the middle between Access and SQL server in
performance).

If after all this you still want to implement my counter on your website, please let me know, it will make
me really happy.

其它资源
来源声明

版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明