<% ' Persistent Web Page Counter, Version 2 ' by John Sgro (John@SucceedOnTheWeb.com) 'declaring variables, arrays Dim x Dim i Dim DisplayCount Dim ThisPage Dim ShortPageName Dim FSObject Dim GetTotals Dim WriteTotals Dim PageHits() Dim PageName() ThisPage = Request.ServerVariables("URL") 'Grabs page path/name from root ThisPage = lcase(ThisPage) 'changes URL to lower case DisplayCount = 0 'Will use later to determine if new page 'Open a FileSystemObject and fetch data from the text file containing our page counts Set FSObject = CreateObject("Scripting.FileSystemObject") 'True creates file if none exists Set GetTotals = FSObject.OpenTextFile("d:\Inetpub\wwwroot\PageTotals.txt",1,True,0) 'Now read each line in our text file and load the data into the PageName and PageHits arrays x=0 While Not GetTotals.AtEndOfStream 'Loop until last line ReDim preserve PageName(x) 'Append PageName array PageName(x) = GetTotals.ReadLine 'Put first named page into PageName array If PageName(x) = ThisPage Then 'Check to see if this page name is current page ReDim preserve PageHits(x) 'Append PageHit array PageHits(x) = GetTotals.ReadLine + 1 'If current page, add 1 to total and put into PageHits array DisplayCount = PageHits(x) 'DisplayCount now contains this page's counter total x = x + 1 'Increment the index number for our arrays Else ReDim preserve PageHits(x) 'If this page is NOT the current page... PageHits(x) = GetTotals.ReadLine 'we still load it into our array, but do not adjust total x = x + 1 'Increment the index number for our arrays End If Wend 'Loop through rest of PageTotals.txt GetTotals.Close 'Clean up Set GetTotals = Nothing 'Routine for new site or newly added pages If x = 0 then '.txt file empty...1st site hit Redim preserve PageName(x) 'Create PageName array item and, PageName(x) = ThisPage 'Array item = this Page's Name. Redim preserve PageHits(x) 'Create PageHit array item PageHits(x) = 1 'Set page's hit total to 1 DisplayCount = 1 'Count to be displayed on page elseif DisplayCount = 0 then 'Or if this page is newly added x = x + 1 'Bump up the array index ReDim preserve PageHits(x) 'Create PageHits array item PageHits(x) = 1 'Set new page's hit total to 1 DisplayCount = 1 'Count to be displayed on page ReDim Preserve PageName(x) 'Create PageName array item and, PageName(x) = ThisPage 'Array item = this Page's name. End If 'Now that we have our arrays loaded, we write a "new" PageTotals.txt file Set WriteTotals = FSObject.CreateTextFile("d:\Inetpub\wwwroot\PageTotals.txt") x = 0 'Reset our array index to 0 For Each PageEntry In PageName 'Looping through our PageName array WriteTotals.WriteLine PageName(x) 'Write the PageName to the text file WriteTotals.WriteLine PageHits(x) 'Next line is the page's hits total x = x + 1 'Increment our array index number Next WriteTotals.Close 'Clean up Set WriteTotals = Nothing Set FSObject = Nothing 'Routine to parse page name from path/file name ThisPage = left(ThisPage,len(ThisPage)-4) For i = 1 to len(ThisPage) ShortPageName = Right(ThisPage,i) If left(ShortPageName,1) = "/" then ShortPageName = Mid(ShortPageName,2,len(ShortPageName)) exit for end if next ThisPage = UCase(left(ShortPageName,1)) & Mid(ShortPageName,2,len(ShortPageName)) 'Now just drop the page count wherever we want in our page Response.Write("<font size=3><b>You are " & ThisPage & " Page visitor number...</b></font><br>" & DisplayCount & "</font>") %>