![]() |
Before we can start writing the ASP for the hit counter we first need to create a few files and graphics. <html> <% 'Create
a File System Object variable 'Initialize
a File Object with the path and name of text
file to open 'Open
the visitor counter text file 'Read
in the visitor number from the visitor
counter file 'Increment
the visitor counter number by 1 'Create
a new visitor counter text file over writing
the previous one 'Write
the new visitor number to the text file 'Reset
server objects 'Display
the hit count as text 'Loop
to display graphical digits </body> 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). Main
Page | Hosting
Plans | E-Commerce
| Sign
Up Now! | Site
Map asp web site hosting by HyperlinkHost.com
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'.
Next we begin writing the code for the ASP Hit Counter. Open up your 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.
<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.
Set fsoObject =
Server.CreateObject("Scripting.FileSystemObject")
Using the 'GetFile' method of the 'File System Object' we initialize 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.
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.
Set tsObject = filObject.OpenAsTextStream
Next use 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 also use the VBScript function 'CLng'
to convert the text from the 'hit_count.txt' into
the data type, long integer.
lngVisitorNumber = CLng(tsObject.ReadAll)
Now we take the number placed into the variable and add one to it.
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.
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.
tsObject.Write CStr(lngVisitorNumber)
We have finished using the server objects so we release them, freeing up server
resources.
Set fsoObject = Nothing
Set tsObject = Nothing
Set filObject = Nothing
Now that we have finished reading in the hit count, updating it, and saving it
back to the server, we need to display the hit count in a web page.
There are a couple of ways to do this; either with just text or with graphics.
First we'll display it as text.
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.
'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.
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.
</html>
And that's it, you have now created a hit counter for a web page.
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.