When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Thursday, July 06, 2000

Improving the Persistent Page Counter


Okay..okay...so who gets it perfect on the first try? Not me, that's for sure!

- continued -

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.asp file automatically add the page and hit total (which would be 1) to the .txt file. 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.txt file 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 .txt file 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:

Set GetTotals = FSObject.OpenTextFile("d:\Inetpub\wwwroot\PageTotals.txt",1,0)

We're going to make a slight change here. Let's simply add a True in there so that we now have:

Set GetTotals = FSObject.OpenTextFile("d:\Inetpub\wwwroot\PageTotals.txt",1,True,0)

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:

' Routine for determining 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 PageHits array item and...
  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 PageName(x)  ' Create PageName array item and...
  PageName(x) = ThisPage      ' Array item = this Page's name.
  ReDim preserve PageHits(x)  ' Create PageHits array item and...
  PageHits(x) = 1             ' Set new page's hit total to 1
  DisplayCount = 1            ' Count to be displayed on page
end if

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.

The 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 PageName and 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 Part 2!

  • Read Part 2

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article