To read the article online, visit http://www.4GuysFromRolla.com/webtech/061600-1.shtml

Creating a Persistent Web Page Counter

By John Sgro


An updated version of this persistent Web counter is available. After reading this article (to learn the nuts and bolts of the counter), be sure to read Improving the Persistent Page Counter, which provides several new enhancements to the persistent Web counter presented in this article!

It seemed like a simple enough request...

A client of mine wanted to have hit counters on every page of his Web site. We didn't want to rely on any of those third-party counters that are loaded with advertising (and don't display half the time anyway!). Okay, just use a small Access database to do the job, right?

Nope. No DB on the server that housed his site! So I came up with this little bit of script to keep track of all of the pages on his site using the Text Stream method of the FileSystemObject. (To learn more about the FileSystemObject be sure to read the FileSystemObject F.A.Q.!) One advantage to using this method as opposed to using application-level variables is that we'll have a "hard copy" of our page counts. So if the server is shut-down or rebooted we won't lose our page totals.

Being a "rookie" ASPer, as well as a first-time 4 Guys author, please bear with me as I may tend to over-comment much of this. But as a beginner, I found that I learned best when even the simple parts of programs were explained.

If you want to take a look at the VERY SIMPLE sample site that we'll be using to display our page hits, check out: http://www.SucceedOnTheWeb.com/WebArticles/PCHome.asp. This page will show a simple output of the hit counter.

Okay, now let's get to it! This method of tracking page counts involves two parts.

    1.) A simple text file that will contain each page name and its associated hit total.
    2.) The actual script that does the "work" and is <!--included--> in each page.

First, let's take a look at our text file that is playing the part of "surrogate database". This .TXT file will store the counter totals for each page. Here is the PageTotals.txt file that we will be using in our example...

/Home.asp
0
/News.asp
0
/SomeDirectory/Links.asp
0

That's it??? Yup. That's all you need for a three-page site. Obviously your site will have more pages (and, therefore, more lines in the text file), but that's not a problem. The PageTotals.txt file can be as long as you want. Just follow a few simple rules: Make sure that no page name is repeated. Each entry MUST be on a separate line. No blank lines and no extra code at the beginning or end of the file. And (contrary to usual practice) NO commenting in this file! Just list each page name followed by that "0" on the next line. Note that every non-numeric line contains the full virtual path to the file you want to count with the page counter. So, if the page is in the root Web directory, the name would be /ASPName. If the page was in the directory named Bob, the name would be /Bob/ASPName.

Now we move to Step #2. Take a look at how a page that wished to display a page count should be constructed...

<html><body>
=== The HTML body "stuff" goes here. ===

<!--#include file="PageCounters.asp"-->	

</body></html>

All that is needed is an <!--Include--> near where we are going to display our counter. This same <!--include--> file is used in every page of our site that we wish to display a page counter for!

In Part 2 we will look at the source for PageCounters.asp, the page that does all of the actual work!

  • Read Part 2


  • Article Information
    Article Title: Creating a Persistent Web Page Counter
    Article Author: John Sgro
    Published Date: Friday, June 16, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/061600-1.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers