Improving the Persistent Page CounterBy
Okay..okay...so who gets it perfect on the first try? Not me, that's for sure!
Shortly after the code for the Persistent Web Page Counter was published on 4Guys a couple of weeks ago, I received an email from Mark S. who made an excellent suggestion:
"Why not put a conditional test in the
PageCounters.asp <!--include-->file that would check to see if a page existed in the text file which contains the hit totals? If it doesn't, have the
PageCounters.aspfile automatically add the page and hit total (which would be 1) to the
.txtfile. That way forgetful programmers like myself wouldn't have to worry about updating page listings and uploading the new text file whenever a page is added to a site."
Great idea Mark! Especially since I fit into that "forgetful programmer" category. :-) With just a minor adjustment and a few lines of code added to our initial script, we are now able to:
1) ...check to see if a
PageTotals.txtfile even exists. If it doesn't, we'll create it from scratch. Now we don't even have to worry about uploading or maintaining the
.txtfile at all. Just forget about it and let the
<!--include-->file handle it!
2) ...see if the page that our visitor just hit is newly added to the site. If it is and the page is not listed, we'll automatically add the page and initialize its hit total.
3) ...resolve differences between page URLs that are typed in as lower case, upper case or mixed case. For example, we want to make sure www.whatever.com/home.asp is handled the same way that
WWW.WHATEVER.COM/HOME.ASP is handled.
4) And as a little extra improvement, we'll parse the page name from the path/file name in our text file so that it can be displayed in a more "elegant" manner to our visitor.
To see the full code of our NEW Persistent Web Page Counter file, download the script source. Since this improvement to our original file does not involve much code at all, we're not going to recap the entire script. We're just going to go over the additional lines of code. So if you have not read the original article, be sure to so that you'll know what we'll be talking about.
Okay? Ready to get started? Let's get to it...
The first thing we'll tackle is a little change in the options we used when opening up our text file. Originally we used this:
We're going to make a slight change here. Let's simply add a
True in there so that we now have:
What does the
True do for us? It allows us to CREATE a
PageTotals.txt file if none exists. So now we don't even have to worry about uploading that
.txt file when we are setting up our counters. It's done for us automatically (gee, isn't that the sweetest sounding word to a programmer...automatically?). That's it for creating our text file! That was easy. Let's move on to the issue of new pages.
This next little snippet of code has been inserted right into the middle of our script. It'll be executed after we've read our info (if there is any) from the text file and before we re-write our new info back into
PageTotals.txt. Here is the snippet we'll be discussing:
All we've got here is a little
if..then conditional code. What we're doing is checking to see what the current value of
x is. Since
x is the index we use to build the arrays that hold our counter info, there is only one way that it can equal 0...if there is NO INFO at all in our
PageTotals.txt file. In other words, this is the VERY FIRST HIT to our site. After giving the person a prize for being our first visitor, we simply pop
ThisPage into our
PageName array and a 1 into our
PageHits array. We also have to set
DisplayCount = 1 for when we want to show our hit total later on. Great! We're covered for the instance of our very first site visitor.
elseIf part of our conditional code comes into play when we get the very first hit to a NEW PAGE on our site. Let's back up just a tad and look at a single line that we added at the top of our script. Right after we declared our variables, we set
DisplayCount=0. This is how we can determine if we are on a new page. The only way that
DisplayCount can still be equal to 0 at this point of the script is if this page was not identified as one already present in our
PageTotals.txt file. If it were already in existence, the
DisplayCount variable would have been set to the appropriate total, right? Now we pretty much just do our normal procedure for adding a page name and hit total into our arrays. Bump up the array index, put
ThisPage into the
PageName array, set the
PageHits array to 1 (since it's the first hit to our new page) and, of course, set our
DisplayCount variable to 1.
That's it! Now the rest of our
PageCounters <!--include--> file performs exactly as explained in our original article. We just loop through our
PageHit arrays and write them back to a new
PageTotals.txt file. The hard work is done. Now there are just two more little tweaks we should make... these tweaks are discussed in