Home arrow ASP arrow Page 2 - ASP Hit Counter
ASP

ASP Hit Counter


In this article Subhendu shows us how to create a very simple but useful hit counter in ASP for the pages of our web site.

Author Info:
By: Subhendu Mohapatra
Rating: 4 stars4 stars4 stars4 stars4 stars / 72
January 15, 2003
TABLE OF CONTENTS:
  1. · ASP Hit Counter
  2. · Building the Counter
  3. · Conclusion

print this article
SEARCH DEVARTICLES

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:

Counter

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
%>

blog comments powered by Disqus
ASP ARTICLES

- Central Scoreboard with Flash and ASP
- Calorie Counter Using WAP and ASP
- Creating PGP-Encrypted E-Mails Using ASP
- Be My Guest in ASP
- Session Replacement in ASP
- Securing ASP Data Access Credentials Using t...
- The Not So Ordinary Address Book
- Adding and Displaying Data Easily via ASP an...
- Sending Email From a Form in ASP
- Adding Member Services in ASP
- Removing Unconfirmed Members
- Trapping HTTP 500.100 - Internal Server Error
- So Many Rows, So Little Time! - Case Study
- XDO: An XML Engine Class for Classic ASP
- Credit Card Fraud Prevention Using ASP and C...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials