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

80酷酷网    80kuku.com

  统计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

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: