网站统计代码 只要包含此代码即可统计

网络整理 - 08-18


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.



原作者:不详
来 源:不详