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.
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:
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:
ID: An auto number that is automatically assigned to each new row added to the table.
Hit_Date: Will store the date that the "hit" was recorded
Hit_Time: Will store the time that the "hit" was recorded
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:
Set Conn = Server.CreateObject("ADODB.Connection")
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.