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

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Thursday, July 06, 2000

Improving the Persistent Page Counter, Part 2


  • Read Part 1

  • 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.

    - continued -

    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:

    response.write("You are " & ThisPage & _
           " Page visitor number...<br>" & DisplayCount)

    Unfortunately, that gave us a line of text at the bottom of our pages that might look something like this:

    You are /website/home.asp Page visitor number...

    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.

    ThisPage = left(ThisPage,len(ThisPage)-4)
    For i = 1 to len(ThisPage)
      ShortPageName = Right(ThisPage,i)
      If left(ShortPageName,1) = "/" then
        ShortPageName = Mid(ShortPageName,2,len(ShortPageName))
        exit for
      end if
    ThisPage = UCase(left(ShortPageName,1)) & _

    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 Link.asp (or Home.asp, News.asp, etc.) when you save and upload it, you'll get:

    You are Link Page visitor number...

    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!

    Happy Programming!

  • John Sgro (John@SucceedOnTheWeb.com)


  • Download PageCounters.asp in text format
  • Read Persistent Web Page Counter

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