日期:2009-09-13  浏览次数:20958 次

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