Alain's Webcounter


This web counter can work as a (javascript based) text counter or as a simple graphical counter. When working as a graphical counter, digit appearance can be configured. It is possible to manage multiple counters by placing counter.cgi scripts in several directories. Specially designed for multiuser sites: a site-wide file can be reused by multiple users by "require"ing it in their own counter.cgi files.


Download from here.


This program needs the Perl module, which can be found on CPAN (cpan -i GD)

Multi-User setup

  1. The administrator should put the counter.cgi script to a place accessible to all users of the system (such as /usr/local/lib/webcounter/

    The administrator should also make sure that the users have the right to run cgi scripts (Set Options ExecCGI in /etc/httpd/httpd.conf), and that those cgi scripts run under the user's privileges (suexec).

  2. Each user then just copies the counter.cgi example from /usr/local/lib/counter/examples/counter.cgi to his Web directory, and customizes it to his taste. The following variables can be set in this script:

Single-user setup

The user needs privilege to run cgi scripts. If so, he can just install into any of his directories, and require it from his counter.cgi file:
require "/home/tom/webcounter/";

Invoking the counter

There are two ways to invoke the counter.
  1. graphical counter (for non Javascript capable browsers, or just because it looks cuter):
    <img src="counter.cgi?W=6">
    W=6 means that six digits should be displayed.
  2. Javascript counter (displays a text counter)
    <script src="counter.cgi?js=1"></script>
  3. Hybrid mode (displays a Javascript text counter where available, and an image counter otherwise)
          <script src="counter.cgi?js=1"></script>
          <noscript><img src="counter.cgi?W=6"></noscript>
  4. Test mode (displays 0123456789, with bars between digits. This is useful for testing variable font width digit files):
    <img src="counter.cgi?test=1">

Technical details

The actual count is kept in a file named counter.txt in the directory where the counter.cgi file resides

Sample digit files

The following pre-written digit files are available in the "digits" directory:
Black font on white background
White font on black background

Making digit files

There are two ways to store digits:
  1. In one file, as a long 0123456789 strip. Example: [classic digit strip] When making such an image, it's best to use a font with a constant width for digits. If you use a variable width font, you need to supply a .pos file as well. This file is a simple text file containing the starting x-coordinate of each digit, and the ending x-coordinate of the last, one per line. Example: a variable width digit strip and its positions file.

    In your counter.cgi file, write: $digits=/home/user/mydigits.jpg. The name of the position file is implied by the name of the digit file: it is obtained by substituting the .jpg extension with .pos. In this example, it would be /home/user/mydigits.pos. For testing your positions file, you might want to use the test mode of the counter.

  2. In a directory, containing a file for each digit: 0.jpg, 1.jpg, ..., 9.jpg. In your counter.cgi file, write $digits=/home/user/mydigitdirectory

Counter in action

SiteRoute - Free Articles, Resources and Forums for Webmasters

Valid HTML 4.01 Transitional

Alain Knaff
Last modified: Mon Apr 9 11:19:42 CEST 2007