Creating a Persistent Web Page Counter, Part 2By John Sgro
In Part 1 we looked at the
PageTotals.txt file, which acted
as a surrogate database, containing all of the page hit count information.
Now onto the actual file that will be included in all our pages and which does all our site counter "work".
First let's take a look at the complete
PageCounters.asp file and then we'll go over it step
by step and explain what it's doing...
The complete code for
PageCounters.asp can be seen
break it down into chunks to see what's it's doing (and so that we can learn how to use some of this stuff in
After declaring our variables and arrays, we set the
ThisPage variable equal to the value of
Request.ServerVariables("URL") contains the full
virtual path to the currently executing ASP page. This variable, then, will indicate what page needs to have
its hit count incremented. Next we create a
FileSystemObject and store it in the
FSObject variable. Now comes the fun stuff! We use the
OpenTextFile method of
FSObject to open up a "text stream" to our
PageTotals.txt file for reading. That
sounds confusing, but all it means is that we now have a method of stepping through each line of our text
file and doing something with the output...very much like the way you would step through each record of a
database after you open up a recordset. Pretty cool, don't ya think?
OpenTextFile takes three arguments. The first is the path to where the
file is located. The second argument is the I/O MODE we are going to use. The
1 simply means that
we are opening the file to READ its output. The third argument is the FORMAT of the text. Using
just specifies that the data is in ASCII format (which is adequate in most cases). We use the
GetTotals variable to get at our text stream data. And now that we have that info from our text
file, let's use it for something!
This next snippet is the part of our script that actually finds which page count we need to adjust and display to our visitor.
While Not GetTotals.AtEndOfStream is pretty straight-forward. All it says is that we're going to
step through our text file until we reach the end.
Next we set up a dynamic array,
PageName(), which will hold all of our site's page names. Don't
forget to use
preserve...we don't want to overwrite our array on the next loop through!
PageName(x) = GetTotals.ReadLine takes the first line of our text file and stores it as the first
element of the array.
Now we are going to see if this is our current page. Remember how we declared a variable called
ThisPage at the top of
PageCounters.asp? We're going to use it now and see if it matches our
PageName(0) element. If it does we have to increase our hit total by 1 and store it in the
ReadLine that we used a minute ago to read our page name did one other thing for us. It
conveniently skipped down to the next line in our text file. We're sitting right at our page hit total! If we
have a match then
PageHits(x) = GetTotals.ReadLine + 1 stores the hit total into our array after
upping it by 1. We also set
DisplayCount = PageHits(x) so that we can display the counter on our
x = x + 1 simply increments our array index so that we can store the rest of our
text file info as we loop through. And once again
ReadLine has dropped us to the next line of our
text file...the next page name on our site.
Let's say we didn't have a page match. We do ALMOST EXACTLY the same thing, except we read the hit total in
without bumping it up by one. (And we don't set our
DisplayCount variable, of course!) So,
PageHits(x) = GetTotals.ReadLine. The count remains the same...
ReadLine puts us at
our next page name.
We keep looping like this until we reach the end of the text stream. All the page names are now loaded into
PageName() array and all the counts are loaded into our
Remember, only ONE total, the count for THIS PAGE was increased by one. Now that we have all our info,
we can clean-up and
Set GetTotals = Nothing. Still with me? I hope so. We're almost done. :)
The final step of our procedure is to take all of our page counter totals and write them back into the text
file so that they are available for the next visitor that hits the site. This isn't hard at all since we have
that info stored in our
PageHits() arrays. And we're going to call
another method of the
FileSystemObject to do the work for us...
Similar to the
OpenTextFile method that we used at the beginning,
FSObject.CreateTextFile allows us to take a stream of data and create a text file out of it.
We'll essentially just be re-creating our original text file with our adjusted page total. As we did with
OpenTextFile, we have to specify the path and name of the file... but that's it. No other
arguments are needed. We will just call it
PageTotals.txt again so that the "old" text file is
overwritten with the new data.
I'm sure you've figured-out what is happening here. We're just looping through both our arrays and letting
WriteTotals.WriteLine list our info back into our text file.
WriteLine is similar to
ReadLine in that after it writes each line of text to the file, it skips down one line before
entering the next piece of data. Voila... a new
And we JUST GOTTA clean-up! :)
And don't forget that we have to show our guest their visitor number. You remember how we stored this page's
new hit total in the
DisplayCount variable way back there when we were having fun loopin' and
readin', right? So just drop the following snippet of code wherever you want on the page.
That's it! We have a way to keep track of all the hits to every page on our site. And we did it all with a
simple text file, our
PageCounters.asp include file. Kinda like a database for people who don't have a database, huh?
I hope my fellow "ASP rookies" learned something from this article. I enjoyed writing it! I'm sure that the pros out there probably know of ways to do some of this stuff much quicker or easier, but sometimes we new guys just have to hack it out whatever way we can, you know? :)
I'd be happy to hear whatever feedback or suggestions you may have...good, bad or otherwise. And THANKS to 4 Guys for giving me a chance to play "teacher" for a little while!
PageCounters.aspin text format