<%@enablesessionstate=false%>
<%Option Explicit
response.buffer=True
'
'------------------------------------------------------------------------- 
'
'  FileName: hitcounter.asp
'  Version : 1.0
' Author(s): Greg Walker
'  LastRev : 27-Mar-2001 Initial Generation
'
'-------------------------------------------------------------------------
          
'The value of the "strCounterName" string holds the value of the calling
'pages Counter Name. The value of the counter for "strCounterName" is
'Incremented and then converted to an integer value and stored in
'"intPageCount" variable.  The WriteCounter Function is then called with
'"strCounterName" (see WriteCounter Function for details) We then send
'a response back to the caller with our custom counterlogo.gif.
'
 Dim strCounterName, intPageCount, strFileName

'The path where hitcounter.asp is located, as well as  
'where all counter files will be read from, and written to
 Const CounterPath = "d:\192.168.0.1\counters\" 
     
'File i/o constants, used in reading and writing
 Const ForReading = 1, ForWriting = 2
     
'Get the Counter Name from the caller's "c_id" query string
 strCounterName = Request.QueryString("c_id")

'Call CounterRead function to get (or create if a new counter) counter's
'string value into CounterRead, convert the CounterRead string value to
'an integer value and increment counter's count by 1 
 intPageCount = (Int(CounterRead(strCounterName)) + 1)

'Write new counter value to both countername.txt and countername.js files
 WriteCounters(strCounterName)

'Send our counterlogo back to caller 
 Response.Redirect ("counterlogo.gif")

 
Function CounterRead(strFileName)
'
'This function accepts a single parameter, strFileName. The function reads
'the calling counter's value from it's counter text file. If the file does
'not exist it will be created and CounterRead will be set to the value of
'DefaultCount. If a counter file does exist the value will be read into
'CounterRead.
'
  'Set DefaultCount used to seed all New Counter Starting Values
   Const DefaultCount = "100"

   Dim strTFileName, objFSO, objTSR 

  'set text counter filename to counter name (i.e. countername.txt)
   strTFileName = CounterPath & strFileName & ".txt"
 
   Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

  'Check if counter file exists 
   If objFSO.FileExists(strTFileName) Then 
      Set objTSR = objFSO.OpenTextFile(strTFileName, ForReading, True)
      CounterRead = objTSR.ReadLine
   Else   
      Set objTSR = objFSO.OpenTextFile(strTFileName, ForReading, True)
      CounterRead = DefaultCount
   End if   

   objTSR.Close
   Set objTSR = Nothing
   Set objFSO = Nothing
End Function
    
   
Function WriteCounters(strFileName)
'
'This function accepts a single parameter, strFileName. The function
'writes to two separate files using the name of the counter, passed in 
'strFileName (the value in strFileName should be the counter's name). The 
'counter's value is written to the counter's name with a ".txt" extension.
'To write the second file the BuildJSInc function is called to get the
'counter's value converted into digits (see BuildJSInc function) and
'finally the BuildJSInc function's return value is written to the
'counter's name with a ".js" extension.
'
   Dim strTFileName, strJFileName, objFSO, objTST, objTSJ 

  'set text counter filename to counter name (i.e. countername.txt)
   strTFileName = CounterPath & strFileName & ".txt"

  'set JavaScipt include filename to counter name (i.e. countername.js)
   strJFileName = CounterPath & strFileName & ".js"
  
   Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

  'write the counter's count value to a counter.txt file using countername
  'as filename:
   Set objTST = objFSO.OpenTextFile(strTFileName, ForWriting, True)
   objTST.WriteLine (intPageCount)

  'write the counter's client-side JavaScript include file using
  'countername as filename:
   Set objTSJ = objFSO.OpenTextFile(strJFileName, ForWriting, True)
   objTSJ.WriteLine (BuildJSInc(intPageCount))

   objTST.Close
   objTSJ.Close
   Set objTST = Nothing
   Set objTSJ = Nothing
   Set objFSO = Nothing
End Function
 
 
Function BuildJSInc(iCount)
'
'This function accepts a single parameter, iCount, and returns an html
'string containing a series of images that represent the graphical image
'of the value in iCount. The html string is wrapped in javscript code in
'order to be used as a client-Side javascript include file. The graphical
'image generated will automatically be brought out to seven digits with
'the required number of leading zeros added to the beginning of the html
'string.
'
   Dim iPadding, iLoop, strResult

  'Begin checks on iCount passed in, should a value be invalid a single
  'gif from the digits used will be returned in BuildJSInc ie x.gif where
  'x is the number of the check that failed. The image will not be seven
  'digits, just a single gif image indicating the check that failed.

  'Check-1 that iCount is a number
   If Not IsNumeric(iCount) Then
      BuildJSInc = "<img src=""counters/images/digits/1.gif"" border=0>"
      Exit Function
   End if   
  'Check-2 that iCount IS NOT a decimal number
   If InStr(1, iCount, ".") Then
      BuildJSInc = "<img src=""counters/images/digits/2.gif"" border=0>"
      Exit Function
   End If   
  'Check-3 that iCount IS NOT a negative number 
   If iCount < 0 Then
      BuildJSInc = "<img src=""counters/images/digits/3.gif"" border=0>"
      Exit Function
   End If          
  'All checks passed begin building Client-Side JavaScript include string
      
   strResult = "document.write('"
      
  'Add required padding of leading zeros to make a seven digit counter  
   For iPadding = (6 - Len(iCount)) to 0 STEP -1
      strResult = strResult & "<img src=""counters/images/digits/0.gif"" border=0>"
   Next    
  'build actual counts based on value of iCount  
   For iLoop = (Len(iCount) - 1) to 0 STEP -1
       strResult = strResult & "<img src=""counters/images/digits/" & _
       Mid(CStr(iCount),Len(iCount) - iLoop,1) & ".gif"" border=0>"
   Next

   strResult = strResult & "<br>')"
   BuildJSInc = strResult
End Function
   
%>


Note: The BuildJSInc function above is a modified version of code from the 4Guys article: Generating Numbers Graphically