ASP
  Home arrow ASP arrow Page 2 - ASP Hit Counter
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

ASP Hit Counter
By: Subhendu Mohapatra
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 23
    2003-01-15

    Table of Contents:
  • ASP Hit Counter
  • Building the Counter
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


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

    More ASP Articles
    More By Subhendu Mohapatra


     

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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek