CrossLink

Server-Side Page Hit Counter

Page hit counters are generally implemented (poorly) with a cgi-bin program that returns a graphic. It looks ugly and doesn't work for people who choose not to load web page images or who have text-only browsers. The other less common method is to use a server-side include executable program which can be insecure and is not very efficient (it requires fork, an extra unix process).

We have a better way. We've added a server side include "counter" function to our web server. Now you can have a page hit counter with none of the traditional problems. In fact, its so efficient that we can offer it to all of our web customers!


How to set it up

First, turn your HTML document into a SHTML document.

The first thing you have to do is adjust the filename extension of your web page so that the server recognizes it as containing server-side includes. Just change the extension to .shtml. For example, if your web page was named "index.html" or "index.htm", your new web page should be named "index.shtml". (Other valid extensions for server-side include HTML documents on the CrossLink web server are: "-s.html" and "-s.htm".)

Next, create a file to hold the value of the counter.

Now, you need to create a file to hold the value of the counter. This is an ASCII text file which contains one line on which the counter value is stored. This file must always have the extension ".cnt". The file can begin with any name, but we recommend that you use the same base name as the .shtml file you intend to use it in. So, if your web page was named, "index.shtml," then name the counter file, "index.cnt". Put this file on the server, with one line containing the number you want the counter to start at.

When you put the counter file on the server, it must have special "access permissions." It must be set read/write by user, group, and other so that the HTTP server can read and update it. You can use the chmod command to do this from your FTP program or from the Unix shell prompt. (Please see our web account access page for more information.)

Finally, add the counter to your SHTML document.

Add the counter directive in your web page where you want the counter to appear. This directive looks like an HTML comment. In its simplest form, its "<!--#counter file="filename" -->". Filename is the base name of the counter file, without the .cnt extension. So, if your counter file is named counter.cnt, the counter directive you would use is "<!--#counter file="counter" -->". There you go, you now have a counter in your page; load up your web browser and try it out!


Advanced Options:

You can do quite a bit more with the counter than just add one to it for each hit. You can keep track of page hits silently, adjust the appearance of the counter, and more! Here is sample displaying the list of options you can give the counter directive:

<!--#counter silent="no" width="0" nocommas="no" min="0" max="2147000000" rollover="no" increment="1" ith="no" gfx="#" file="counter" include="file%i.html" debug="" -->

Where:

silent:
Whether or not to show the value of the counter to the person accessing the page. yes - Don't show the counter. no - Show the counter (default).
width:
The minimum width of the displayed counter (padded with 0's if necessary).
nocommas:
Whether or not to insert commas into the number for readability. yes - Don't insert commas. no - insert commas (default). If width is not 0 then no commas are inserted regardless of the setting in nocommas.
max:
The maximum number to count to. When the counter reaches this number, its behavior is controlled by rollover. By default the number is near two billion.
min:
The minimum number to count down to. When the counter reaches this number, its behavior is controlled by rollover. By default the number is zero.
rollover:
When you hit the maximum or minimum allowed number, what do you do? yes - Flip to the other endpoint. (I.e., if at max, change to min, if at min, change to max.) no - Stop counting. Do not add or subtract any further from the counter.
increment:
The number to add to the counter. Default: 1. You may also use the keyword random which means that the next value of the counter should be a random number between min and max.
ith:
Whether or not to append "st", "nd", "rd", or "th", as appropriate to the end of the counter (i.e. 1st, 2nd, 3rd, 4th, etc). yes - Add the extension. no - Don't add the extension (default).
gfx:
Use graphics (i.e. GIF files) to display the counter number instead of using a text counter. Put the HTML markup to include the graphics where "#" is the digit to replace. For example, if your counter were at 1,234 and you used the following line: <!--#counter ith="yes" gfx="<IMG SRC=\"gifs/#.gif\">" file="counter" --> then it would be replaced by the HTML markup: <IMG SRC="gifs/1.gif"><IMG SRC="gifs/,.gif"><IMG SRC="gifs/2.gif"><IMG SRC="gifs/3.gif"><IMG SRC="gifs/4.gif">
file:
The name of the file (minus the .cnt extension) to store the counter in. (For example, if your filename is home.cnt then you should use file="home".) Warning: the file directive must come after everything except include and debug.
include:
The "base" filename (where %i is the counter) of a file to include in the document based on the value of the counter. (For example, if you want to include pageX.html where X is the value of the counter, use include="page%i.html".)
debug:
If it exists, print out the internal variables at the point in the command to assist in figuring out why the counter isn't working. This is provided for expert users... If you don't understand what its saying, don't use it.

Enjoy!


Suggested Uses:

Basic hit counter

Add this to your web document:
Welcome, you are the <strong><!--#counter ith="yes" file="counter" --></strong> person to access this web page!

And create the counter.cnt file.

The result looks like this:
Welcome, you are the 1,234,567th person to access this web page.


Revision History

vA1.1.1-2

vA1.1.1-1

v1.5.1-1

v1.5a-1

CrossLink | Customer-Service Home | Search FAQ | Network Status | Contact Tech-Support

CrossLink Internet Services - 5537 C Hempstead Way, Springfield, VA 22151
703-642-1120 | 1-888-4-CrossLink | FAX: 703-642-1258
web@crosslink.net

©1999-2003 CrossLink Internet; all services subject to our Acceptable Use Policy