欢迎光临
我们一直在努力

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

建站超值云服务器,限时71元/月

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.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 网站统计代码-只要包含此代码即可统计
分享到: 更多 (0)