Home arrow ASP arrow Page 2 - A Simple Way To Tally Your Visitors
ASP

A Simple Way To Tally Your Visitors


Hit counters are used by all types of web sites and allow us to keep an active tally of how many visits our site receives on any particular day. In this article, Borag's going to teach us how to create a simple hit counter that we can use on our personal or small business web sites.

Author Info:
By: Burag Cetinkaya
Rating: 5 stars5 stars5 stars5 stars5 stars / 19
January 20, 2002
TABLE OF CONTENTS:
  1. · A Simple Way To Tally Your Visitors
  2. · Creating our hit counter
  3. · Displaying the hit counter
  4. · Conclusion

print this article
SEARCH DEVARTICLES

A Simple Way To Tally Your Visitors - Creating our hit counter
(Page 2 of 4 )

Let's start by opening Microsoft Access 2000. Create a new access database with the file name hit_counter.mdb and save it in the your web browsers root directory. Create the following table named "Hits" as part of that database:

The design of our hits table

Our hits table will be used to store the details of each individual visit to our site from one user on any particular day. In our case, a "user" is a unique I.P. address. The hits table contains four fields:
  1. ID: An auto number that is automatically assigned to each new row added to the table.
  2. Hit_Date: Will store the date that the "hit" was recorded
  3. Hit_Time: Will store the time that the "hit" was recorded
  4. Hit_IP: Will store the I.P. address of the user for which the "hit" was recorded.
The code to both record and tally hits on any unique day will be stored in one single file named hit_counter.asp. Every time a new, unique visitor (differentiated by their I.P. address) comes to our site on any particular day, a new record is added to the hits table to record the date, time and their I.P. address. The code to do this should be placed in hit_counter.asp and looks like this:

<%

Dim Conn

Dim RS

Dim TotalVisited

Dim ImgSequence

Dim CharToReplace

Dim Query

Set Conn = Server.CreateObject("ADODB.Connection")

Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("hit_counter.mdb")

Set RS = Server.CreateObject("ADODB.Recordset")

Query = "SELECT * FROM Hits WHERE Hit_IP='" & Request.ServerVariables("REMOTE_ADDR") & "' AND Hit_Date=#" & DATE() & "#"

RS.Open Query, Conn, 3, 3

'If this is the first visit from this IP

IF RS.EOF AND RS.BOF THEN

RS.AddNew

RS("Hit_Date") = Date()

RS("Hit_Time") = Time()

RS("Hit_IP") = Request.ServerVariables("Remote_Addr")

RS.Update

END IF

RS.Close

%>


We're using an ADO connection object to connect to our Microsoft Access database using a query string, which specifies the driver type and physical location of our database file. We build a query that will retrieve any rows in our hits table where the Hit_Date field matches today's date, and where the visitors I.P. address matches the Hit_IP field in the hits table. The value of the REMOTE_ADDR variable in the Request.ServerVariables collection holds the I.P. address of the visitor. We assign our query to a variable named query. The query looked like this when I accessed the hit_counter.asp file from my local machine on the 20th January 2002:

SELECT * FROM Hits WHERE Hit_IP='127.0.0.1' AND Hit_Date=#20-Jan-02#

This is only one part of our counter script, however, we can still run it through our web browser to see some results. The first time you run it, a new record will be inserted into the hits table of our hit_counter.mdb database. If, however, you hit refresh in your browser, then you’ll notice that a second record has not been added. As described earlier, only one record per unique I.P. address per day is added to the hits table. This allows us to accurately track how many unique visitors our site has received.
blog comments powered by Disqus
ASP ARTICLES

- Central Scoreboard with Flash and ASP
- Calorie Counter Using WAP and ASP
- Creating PGP-Encrypted E-Mails Using ASP
- Be My Guest in ASP
- Session Replacement in ASP
- Securing ASP Data Access Credentials Using t...
- The Not So Ordinary Address Book
- Adding and Displaying Data Easily via ASP an...
- Sending Email From a Form in ASP
- Adding Member Services in ASP
- Removing Unconfirmed Members
- Trapping HTTP 500.100 - Internal Server Error
- So Many Rows, So Little Time! - Case Study
- XDO: An XML Engine Class for Classic ASP
- Credit Card Fraud Prevention Using ASP and C...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials