ASP Hit Counter - Building the Counter
(Page 2 of 3 )
A few lines of ASP code will create the counter for us. This code can be kept inside the page of our interest or can be stored in a separate file (eg. counter.asp) which we can be executed from any page. We require a text file to store our counter readings (eg. counter.txt).
Our code will read the value from this text file and will update this text file with new incremented value. A write permission is required for this directory to update the text file.
First create counter.txt file and store any initial value in this. Now we will create some variables and use the MapPath method to convert a virtual path into a physical path.
<%
Dim strPath, File, file2, x,liCount, j ,resfile, d, i ,fl
strPath = "counter.txt"
strPath = Server.MapPath(strPath) We will use the file system object to read and write our counter.txt file.
Set File = Server.CreateObject("Scripting.FileSystemObject")
set file2=File.OpenTextFile(strPath) This will create an instance of the file system object and a file object to access the file represented by strPath.
x=file2.readline
x holds the old value of the counter
set resfile = File.createTextFile (strPath)
resfile.writeline(x+1) This will open the counter file for writing and the value x+1 is written back to it. We can display the value using Response.Write(x+1) but we will display them in a more attractive way. For this we have to create some graphical digits from 0 to 9 and store them in appropriate files like digi0.gif, digi1.gif, digi2.gif, digi3.gif, etc. to digi9.gif.
This can be created by using any graphics software or can be downloaded from various web sites. We have to count the number of digits our counter value has and to do this we have to first convert our numeric value to a string.
liCount=Cstr(x+1)
j = Len(liCount) We have to decide how many digits our counter will have in total and we will store the difference between this and the value of j in another variable d. The value in d is the number of zeros we have to display to the left of our counter value. For example to display a value of 435 in a 9 digit counter we have to place 6 zeros to the left of 435 and our counter should display 000000435.
d=9 - j
For i=1 to d step 1
Response.Write ("<IMG SRC='digi0.gif'>")
Next This will display digi0.gif (digit 0) d number of times. Now it’s time to display our counter value.
For i=1 to j step 1
fl="digi" + Mid(liCount, i , 1) + ".gif"
Response.Write ("<IMG SRC=") & (fl) & ">"
Next This will select the digits corresponding with our counter value and display them from left to right. Finally our counter will look something like this:
If you are good with graphics then you can design very cool digits matching the rest of your web site.
<%
Dim strPath, File, file2, x,liCount, j ,resfile, d, i ,fl
strPath = "counter.txt"
strPath = Server.MapPath(strPath)
Set File = Server.CreateObject("Scripting.FileSystemObject")
set file2=File.OpenTextFile(strPath)
x=file2.readline
set resfile = File.createTextFile (strPath)
resfile.writeline(x+1)
liCount=Cstr(x+1)
j = Len(liCount)
d=9 - j
%>
<%
For i=1 to d step 1
Response.Write ("<IMG SRC='digi0.gif'>")
Next
For i=1 to j step 1
fl="digi" + Mid(liCount, i , 1) + ".gif"
Response.Write ("<IMG SRC=") & (fl) & ">"
Next
%>Next: Conclusion >>
More ASP Articles
More By Subhendu Mohapatra