<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Session_OnStart
' Constants for FileSystemObject
Const ForReading = 1, ForWriting = 2
' Get the physical path of the counter file. The counter file is in the root of
' the web site.
sPath = Server.mapPath("/counter.txt")
' Lock the application
Application.Lock
' Create a file system object and open the count file for reading
Set fs = CreateObject ("Scripting.FileSystemObject")
Set CountFile = fs.OpenTextFile(sPath, ForReading)
' Read the first line of the count file, trim any spaces
sVisits = Trim(CountFile.Readline())
' Close the counter file
CountFile.Close
' Increment the count by 1
lVisits = CLng(sVisits) + 1
' Optional: Set this count in an Application variable
Application("Visits") = lVisits
' Open the count file for writing
Set CountFile = fs.OpenTextFile(sPath, ForWriting)
' Write the new count
CountFile.Writeline(lVisits)
' Close the counter file
CountFile.Close
' Unlock the application object
Application.Unlock
' Release File system object and counter file
Set Fs = Nothing
Set CountFile = Nothing
End Sub
</SCRIPT>
Figure 2 The Visits/Visitors Counter
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
' Constants for FileSystemObject
Const ForReading = 1, ForWriting = 2
' Get the physical path of the counter file. Here the counter.txt is in the
' root of the web site
sPath = Server.mapPath("/counter.txt")
' Create a file system object and open the counter file for reading
Set Fs = CreateObject ("Scripting.FileSystemObject")
Set CountFile = Fs.OpenTextFile(sPath, ForReading)
' Read the two lines of the counter file, trim any spaces
sVisits = Trim(CountFile.Readline())
sVisitors = Trim(CountFile.Readline())
' Close the counter file
CountFile.Close
' Set the count of visits and visitors in Application variables
Application("Visits") = CLng(sVisits)
Application("Visitors") = CLng(sVisitors)
' Release File system object and counter file
Set Fs = Nothing
Set CountFile = Nothing
End Sub
</SCRIPT>
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Session_OnStart
' Constants for FileSystemObject
Const ForReading = 1, ForWriting = 2
' Constant for write interval. Change this to suit your needs
Const cWriteInterval = 100
' Get the physical path of the counter file. Here the counter.txt is in the
' root of the web site
sPath = Server.mapPath("/counter.txt")
' Get the cookie from the visitor. The VISITORID cookie may have been set
' in which case sVisitorID will contain a value
sVisitorID = Request.Cookies("VISITORID")
' Lock the application to increment the counts
Application.Lock
' Increment the visits by 1. This happens every time a new session is
' created
lVisits = Application("Visits") + 1
' The number of visitors is obtained. It is incremented later in the code
lVisitors = Application("Visitors")
' Check if the VISITORID cookie exists. If it doesn't, set a new cookie with
' the value of visitor number. This cookie expires after 30 days.
If Len (sVisitorID) = 0 Then
lVisitors = lVisitors + 1
Response.Cookies("VISITORID") = lVisitors
Response.Cookies("VISITORID").Expires = CDate(Date+30)
End If
' Write the counts into the counter file at cWriteInterval hits
If ((Application("Visits") Mod cWriteInterval) = 0) Then
' Create a file system object and open the counter file for writing
Set Fs = CreateObject("Scripting.FileSystemObject")
Set CountFile = Fs.OpenTextFile (sPath, ForWriting)
' Write the new counts into counter.txt
CountFile.Writeline(lVisits)
CountFile.WriteLine(lVisitors)
' Close the counter file
CountFile.Close
' Release File system object and counter file
Set Fs = Nothing
Set CountFile = Nothing
End If
' Set the count of visits and visitors in Application variables
Application("Visits") = lVisits
Application("Visitors") = lVisitors
' Unlock the application object
Application.Unlock
End Sub
</SCRIPT>