Its assumed 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 you can download the images in the attached 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.
<body bgcolor="white" text="black">
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
'Create a File System Object variable
Set fsoObject = Server.CreateObject("Scripting.FileSystemObject")
'Initialise a File Object with the path and name of text file to open
Set filObject = fsoObject.GetFile(Server.MapPath("hit_count.txt"))
'Open the visitor counter text file
Set tsObject = filObject.OpenAsTextStream
'Read in the visitor number from the visitor counter file
lngVisitorNumber = CLng(tsObject.ReadAll)
'Increment the visitor counter number by 1
lngVisitorNumber = lngVisitorNumber + 1
'Create a new visitor counter text file over writing the previous one
Set tsObject = fsoObject.CreateTextFile(Server.MapPath("hit_count.txt"))
'Write the new visitor number to the text file
'Reset server objects
Set fsoObject = Nothing
Set tsObject = Nothing
Set filObject = Nothing
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
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(Mid(lngVisitorNumber, intWriteDigitLoopCount, 1) & ".gif""")
Response.Write("alt=""" & Mid(lngVisitorNumber, intWriteDigitLoopCount, 1) & """>")
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.
If you find that the script is not running correctly and is either not displaying a counter number or not incrementing the number then check that you have sufficient permissions to allow ASP scripts write as well as execute.