Site statistics code-just include this code to count

Source: Internet
Author: User
Tags contains count include
Statistics Have you ever tried to use one of the many fancy, Hightech, multifeatured, superfast and custom made Web
Server statistics? I don't know about you, but I did. Every time I looked the 20+ page, I was
Pretty sure that somewhere between thousands of lines, there must is simple answer to my questions:how
Many times is my page downloaded? After few tries I gave up and wrote my Own, self administrated,
blindingly obvious server statistics.

Idea behind this code are very simple, in order to know what are interesting to visitors the My web site I
Have implemented this simple counter to the each page which is interesting to me. If you examine your site,
Can easily find this 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 the are used for redirecting, storing data into databases, trigering mail
Components etc. So, my counter is suposed to is present on all pages which have some user viewable
Content. All your have to be just to include file which contains counter code whenever
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 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 the My counter database. If there is no
The record with this exact URL (lines), I am assuming the This is new page and I am inserting new
Records in the database and setting counters to one (OK, this is obviously the this page
Viewed). In the case there be record with given URL (line) I am simply increasing counter values by one
(lines to 23). After this, take it easy, close your connections and destroy used objects (lines
). 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
One if Countrs.recordcount < 1 Then
Sqlstr= "INSERT into Counter"
Sqlstr= sqlstr & "(URL, total, today, month, last, yesterday) VALUES"
Sqlstr= sqlstr & "(' & URL &" ', 1, 1, 1, 0, 0) "
15
Count. Execute (SQLSTR)
17
ElseIf countrs.recordcount > 0 Then
Countrs. Fields ("Today") = CInt (countrs. Fields ("Today")) + 1
Countrs. Fields ("month") = CInt (countrs. Fields ("month")) + 1
Countrs. Fields ("total") = CLng (countrs. Fields ("Total")) + 1
22
Countrs. Update
End If
25
Countrs. Close
Count. Close
28
Set countrs = Nothing
Set count = Nothing%>




There is also another file called Report.asp which would simply render data from your database in one
Table. The File is really simple and it renders results from your database into table. I am Creating a
Table (lines 1), opening database connection and selecting all records (lines), setting
Total counter and is month counter to 0 (lines), and starting did while loop until I hit end of
File (line 22). In the table there are three columns, one with URL (= =), second with total number
Hits and third with total number of hits this month (line 34). Note I am also increasing
Values for the total counters to each column (lines and 34). After I exit the My loop I am closing database
Connections and destroying used objects (lines to 45). If you check values in variables ' total '
and "Month" you'll get total number page loading and total number of page loading in current month.


Code Snippet Nr. 3-file



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.