How To Do Counters

by Mark S. Hutchenreuther

Revised 13 February, corrected the code snippet.

Counters are very easy to implement IF your system is set up for them. Counters are implemented as something called a "server-side include." The person accessing your home page sees something like:

You are the 97th person to access this page.

If he or she looks at the source, all they see is exactly that, they do not see the actual source code that inserts the correct count into the expression. I will explain how we do it here at RAIN, your system may be different. I will also direct you to another interesting site with counters.


How We Do Counters At RAIN

On our Web server is a subdirectory called "httpd," which itself contains a subdirectory called "cgi-bin." Residing in the "cgi-bin" subdirectory is a PERL script file called "counterord," and another called simply "counter." The one you are interested in is "counterord," since it is the one that figures out the correct "ordinal" number. (Yes, "counter" figures out the correct "cardinal" number.)

Whenever someone accesses your page, a snippet of inline code (the "server-side include") is executed. This code calls "counterord" and figures out how many people have accessed your page, expresses this as an ordinal number, and inserts it into your home page text.

There are a number of ways of determining if your Web server has the script file in the "cgi-bin" subdirectory. You can:

Ask your webmaster personally or via e-mail
Do an "ls" of "cgi-bin" at the operating system prompt after you find it
Insert the code snippet in your home page and see if it works

If you do not have this neat program, ask your webmaster about providing it in the future. Do not be surprised if he or she says no, there are some security issues with "server-side includes" as well as forms support.


The Server-Side Include

First of all, you need to insert a line of text in your home page, the one that will note the count of accesses. It should say something like:

Welcome. You are the CODE person to access my home page.

The word CODE must be the following expression. Note that this code snippet looks a little like an HTML HREF, and a little like a PERL command line:

<!--#exec cgi="/cgi-bin/counterord"-->

Note that you should use the greater-than and less-than symbols in your file, I have to use the ampersand equivalents here or the server will merely run the command, and you will never see it.

And that's all there is to it, providing you have everything set up in "cgi-bin."


Also, check out the usage statistics for the Beer Page.

Mark S. Hutchenreuther is hutch@rain.org

Return to my home page.