Hit Counter Tutorial

This and other Web Tutorials can be found at: - http://www.webwizguide.com
© Copyright 2001 Web Wiz Guide

Author: Bruce Corkhill
Difficulty: Medium Low
If you are reading this page then I shall assume that you already know a little bit about ASP and running ASP scripts.

Before we can start writing the ASP for the hit counter we first need to create a few files and graphics.

First create a text file in note pad, called 'hit_count.txt' containing the digit 0, and save it in the same directory as you are going to place the page containing the hit counter script (make sure there are no other characters in the file).

Next if you are going to be using graphics to display the hit count in a web page you need to create a folder in the directory you have placed the 'hit_count.txt' text file in called 'counter_images' and place 10 gif images in it with the names, '0.gif', '1.gif', '2.gif', .......... '9.gif'. If you don't want to create your own there some with this zip file.

Now we got that out the way we can begin writing the code for the ASP Hit Counter. Open up you favourite text editor and type in the following code.

As the hit counter is displayed within a web page we first need to start with the HTML for the web page.
<html>
<head>
<title>Hit Counter</title>
</head>
<body bgcolor="white" text="black">

Now we can start writing the ASP. First we need to dimension the variables we are going to be using.
<%
'Dimension variables
Dim fsoObject                   'File System Object
Dim tsObject                    
'Text Stream Object
Dim filObject                    
'File Object
Dim lngVisitorNumber
         'Holds the visitor number
Dim intWriteDigitLoopCount 
'Loop counter to display the graphical hit count

To be able to manipulate the text file used to store the hit count we need to use the Microsoft Scripting Runtime object the 'File System Object'. With this object we can read from and write two files on the web server. In the line below we instantiate the File System Object.
'Create a File System Object variable
Set fsoObject = Server.CreateObject("Scripting.FileSystemObject")

Using the 'GetFile' method of the 'File System Object' we initialise the 'File Object' with the text file containing the hit count. To get the 'hit_count.txt' text file we need to use the physical path on the server to the file. To do this we use the ASP 'Server' object and the 'MapPath' method to get the path to this script and as it is to be saved to the same directory as the 'hit_count.txt' file we can use this as the physical path to the file.
'Initialise a File Object with the path and name of text file to open
Set filObject = fsoObject.GetFile(Server.MapPath("hit_count.txt"))

Once the File Object has been initialise with the 'hit_count.txt' file we can then create a 'TextSteam Object' that we can use to read, create, and write too the 'hit_count.txt' text file.
'Open the visitor counter text file
Set tsObject = filObject.OpenAsTextStream

Next using the 'TextSteam Object', we created in the last line, and the 'ReadAll' method to read the contents of the 'hit_count.txt' text file into a variable. We are also using the VBScript function 'CLng' to convert the text from the 'hit_count.txt' into the data type, long integer.
'Read in the visitor number from the visitor counter file
lngVisitorNumber = CLng(tsObject.ReadAll)

Now we take the number placed into the variable and add one to it.
'Increment the visitor counter number by 1
lngVisitorNumber = lngVisitorNumber + 1

Using the 'CreateTextFile' method of the 'File System Object' we create a new text file called 'hit_count.txt' over writing the original text file so that we can save the new hit count to the file. Again we are using the ASP 'Server' object and the 'MapPath' method to get the path to the script.
'Create a new visitor counter text file over writing the previous one
Set tsObject = fsoObject.CreateTextFile(Server.MapPath("hit_count.txt"))

Next using the 'TextSteam Object' we write the new hit count to the 'hit_count.txt' text file. We are also using the 'CStr' VBScript function to convert the long integer number back into a string.
'Write the new visitor number to the text file
tsObject.Write CStr(lngVisitorNumber)

We have finished using the server objects now so we can release them freeing up server resources.
'Reset server objects
Set fsoObject = Nothing
Set tsObject = Nothing
Set filObject = Nothing

Now we have finished reading in the hit count, updating it, and saving it back to the server we now need to display the hit count in a web page.

I'm going to explain two ways of doing this, either with just text, or with graphics. First I'm going to show you how to display it as text as this is the simplest way.

To display the hit count as text we display the value held in the hit count variable using the ASP 'Response' object and the 'Write' method to write the hit count to the HTTP stream to display the value in the web page.
'Display the hit count as text
'Response.Write(lngVisitorNumber)

If you wish to show the hit count in a graphical format then you need to replace the line above with the following.

Here we use a 'For.....Next' loop to display each digit in the hit count. Using the VBScript 'Len' function to get the length of the hit count number so we know how many times to loop round (eg. If the hit count was '9999' we would need to loop round 4 times to display each digit in the number).

Within the loop we use 'Response.Write' again to write to the web page. To choose which 'gif' image we are going to display we use the VBScript 'Mid' function to find the which digit in the hit count we are displaying in this iteration of the loop.
'Loop to display graphical digits
For intWriteDigitLoopCount = 1 to Len(lngVisitorNumber)

    'Display the graphical hit count
    Response.Write("<img src=""counter_images/")
    Response.Write(Mid(lngVisitorNumber, intWriteDigitLoopCount, 1) & ".gif""")
    Response.Write("alt=""" & Mid(lngVisitorNumber, intWriteDigitLoopCount, 1) & """>")

Next
%>

Finally we need to finish the HTML for the web page we are displaying the hit count in.
</body>
</html>
Now save the file with any name you like in the same directory as the 'hit_count.txt' text file (remember to give this file the extension .asp).

And that's it, you have now created a hit counter for a web page, remember to run the page through an ASP enabled web server.

© Copyright 2001 Web Wiz Guide