bw-counter
This is a simple hit counter for your web page.
It is run from a server-side include (SSI).
On an Apache server you put something like this in your page:
<!--#include virtual="/bw-counter/bw-counter.cgi" -->
The result should be something like this:
46,597
You will need to create a file for the count, and make sure the web server
has read and write permission for that file (i.e., chmod 666 filename
on a Unix system). The full path to that file should go in the $count_file
variable near the top of the script.
Here is the source code for bw-counter.cgi. Copy/paste this code into your
text editor, and make sure that the line
#!/usr/bin/perl -w
is the first line in the file.
#!/usr/bin/perl -w
#
# bw-counter.cgi
#
# Create an incremental counter for a web page.
#
# Copyright 2003 by Bill Weinman [http://bw.org/]
#
# The author grants permission to use and modify this program as you like.
# All other rights and ownership are reserved by the author.
#
use strict;
use IO::File;
use Fcntl ':flock';
my $count_file = "/full/path/to/count";
# minimal valid mime header
print "Content-type: text/html\n\n";
my $count = readfile($count_file);
chomp $count;
$count ++;
print comma($count);
writefile($count_file, "$count\n");
exit;
sub error
{
print shift;
exit;
}
sub comma
{
my $number = shift;
$number += 0; # make sure it's a number;
while ($number =~ s/ ( .*\d ) ( \d \d \d ) /$1,$2/xg) { }
return $number;
}
sub readfile
{
my $filename = shift or return;
my $f = new IO::File("<$filename");
error("cannot open $filename ($!)") unless $f;
my $string = join('', <$f>);
$f->close;
return $string;
}
sub writefile
{
my $filename = shift or return;
my $data = shift || '';
my $f = new IO::File(">$filename");
error("cannot write $filename ($!)") unless $f;
flock($f, LOCK_EX);
$f->print($data);
flock($f, LOCK_UN);
$f->close;
}
|