How to make access counter

There are many free access counters in INTERNET. So you don't have to make it by yourself.
But sometimes these counters have to attach commercial banners in your website. If you don't like commercial banners, perhaps you don't want to use these counters.
When you make it by yourself, there's no such banners.(^^)
If you borrow some part (displaiy numbers part) from another program, it is not so difficult to make it.
So let't make it.

SUPPLEMENTATION: I wrote this chapter in 2005. It pasts almost 10 years. Now I don't use access counter in my website.
This is because access counters need to use CGI. And CGI is sometimes not good in the meaning of security.

What is access counter ?

I say in one word

access counter is graphic program.

As you know, if you want to display some image in html, you should do like below.

<img src="hoge.jpg">

Above script displays hoge.jpg. (graphic way) Access counters use this method. In order to display counter number, you do like below.

<img src="http://hogehoge/">

This (above) cgi script shows numbers to display. In this example the name of cgi script is We assume this is perl script. does works shown below.

Simple procedure !

Read count number from file

This part is shown below.

open HD, "+<", $datafile || die;
flock (HD, 2) || die;
$count = <HD>;
seek (HD, 0, 0) || die;
truncate (HD, 0) || die;
print HD $count;
close HD || die;

I showed just only primary (or basic) part of program.

Simple !

Change count number to image files

You should prepare image files (0 - 9) and the program that shows these images. May be, you can find these in INTERNET.
But there is sometimes copyright protect. So you can get these from books. May be, image files and the program come with books that describes "how to make access counter" .

$count = sprintf ( "%06d", $count);

for ( $i = 0; $i<length($count); $i++){
  $n = substr($count, $i, 1);
  push( @gif_files, "$imagepath$n.gif");

Above program changes number to image files array. Array is @gif_files. Each image of number (0 - 9) is $n.gif.
Example 1234 is changed to 001234. This time I use 6 decimal.
Next is to show these image files.

print "content-type:image/gif\n\n";
print &gifcat::gifcat(@gif_files);

First line is html style. This shows next data is image file. Second line changes mode to images.
Third line shows these images. Readme of gifcat says this program is for Windows. But this program also do well in UNIXs.
To make gifcat is difficult, so I used the file made by another person.
gifcat shows N decmal number (image files) to display.

Refuse pranks and practical jokes from INTERNET

I recommend to make safe guard from pranks and practical jokes from INTERNET. This means continuous access.
Continuous access make access counter strange!! It's prank or practical joke!!
This safe guard is shown below.

$time_file = "./data/time-log"; 
$ip_file = "./data/ip-log"; 
$delta = (5 * 60); 
$now_t = time; 
open FH, "+<", $time_file || die; 
flock FH, 2 || die; 
$last_t = <FH>; 
seek (FH, 0, 0) || die; 
truncate (FH, 0) || die; 
print FH $now_t; 
close FH || die; 
$now_ip = $ENV{'REMOTE_ADDR'}; 
open FH, "+<", $ip_file || die; 
flock FH, 2 || die; 
$last_ip = <FH>; 
seek (FH, 0, 0) || die; 
truncate (FH, 0) || die; 
print FH $now_ip; 
close FH || die; 
if(($now_t - $last_t) >= $delta){ 
  $inc = 1; 
}elsif($now_ip ne $last_ip){ 
  $inc = 1; 
  $inc = 0; 

Time-log keeps access time before. Ip-log keeps access address before.
If next access occurs, check the difference of time and address.

$inc is the key to change counter or not. If it is 1, program increase counter. If it is 0, program does not change counter.
Here is this program . Please use this if you like this.

I got program from the book shown below. In the top of files in CD-ROM comes with book, there is the file that shows copyright of this program. You should read this carefully !
The name of this book:[Perl/CGI] ----- writer:[Hashi] ----- Published from [Mainichi communications]