Hit Counter TutorialThis and other Web Tutorials can be found at: - http://www.webwizguide.info
ï¿½ Copyright 2001 Web Wiz Guide
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.
Now we can start writing the ASP. First we need to dimension the variables we are going to be using.
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.
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.
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.
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.
Now we take the number placed into the variable and add one to it.
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.
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.
We have finished using the server objects now so we can release them freeing up server resources.
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.
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.
Finally we need to finish the HTML for the web page we are displaying the hit count in.
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-2002 Web Wiz Guide