Improving the Persistent Page Counter, Part 2By
In Part 1 we looked at improving
PageCounters.asp so that we didn't have to fuss about adding new lines to
PageTotals.txt when we want new Web pages to be counted by our persistent Web counter. In this part we'll examine some additional tweaks to the script that add extra functionality.
When experimenting with our new script, I realized something about our procedure for grabbing our page names using
Request.ServerVariables("URL"). We weren't taking into account the fact that some people may type
www.whatever.com/home.asp to get to our page while others may type
www.WhatEver.Com/HOME.ASP. Our counter was recording these hits as if they were going to two different pages. That's not what we want. So we'll fix that little bug by just using
ThisPage = lcase(ThisPage) to change our string to lower case right after we grab it at the top of the script. By changing all our page names to lower case before working with them, we've eliminated the problem of mixed-case URLs being counted as different pages. And finally...
In our first version of the Persistent Web Page Counter, we printed out our page hit total with this line of code:
Unfortunately, that gave us a line of text at the bottom of our pages that might look something like this:
Obviously it would be something different depending on the location of the site's files on your server directory. But that
/website/Home.asp part just doesn't cut it. So this final little snippet of code will parse the
ThisPage string that we read from the
.txt file and give us the actual page name without the path information.
This bit of code is placed immediately before the
Response.Write that we just mentioned. Since this article isn't really about string manipulation, suffice to say that it simply hacks-off the
.asp extension, looks backwards character by character until it finds a backslash (
/), and then capitalizes the resulting page name. So if you name your page
News.asp, etc.) when you save and upload it, you'll get:
Definitely a lot nicer looking, don't you agree?
With our improved code, you simply have to upload the
PageCounters.asp file along with the rest of your site files and make sure it's
<!--included--> on each page. Adding a new page? Just upload it! It will automatically be tracked and counted. No more manual
.txt file creation or maintenance. :-)
Moral of the story for other beginning programmers: If possible, let people see your work. I have to admit that I never thought about making this improvement until Mark S. suggested it in his email to me after he saw the original code on 4GFR. His suggestion resulted in a script that, I think, is a 100% improvement over the original. Sometimes you just don't hit the nail right on the head with your first try!
Hope you like the easier-to-use Persistent Web Page Counter, Version 2. If you have any questions, comments or other suggestions, feel free to contact me. I'd love to hear from you!
PageCounters.aspin text format