I'm attempting to add a very simple hit counter to my web page. The source is from the mouse book,
CGI Programming with Perl by Scott Guelich, et al (O'Reilly & Associates, Inc).
It seemed like a fairly safe project. Unfortunately, the code always returns 1. I've double checked permissions, and am pretty sure they are correct. Any help on correcting this would be appreciated.
The prototype web page is simple. The CGI output is called via SSI with the statement <!--#include virtual="/cgi-bin/counter.cgi">.
The CGI code is almost straight from the book, with a correction made on the DBM file path and the USE statement listed in the errata.
#!/usr/bin/perl -wT
#use strict;
use Fcntl qw( :DEFAULT :flock );
use DB_File;
use constant COUNT_FILE => "/home/httpd/data/count.dbm";
my %count;
my $url = $ENV{DOCUMENT_URI};
local *DBM;
print "Content-type: text/plain\n\n";
if (my $db = tie %count, "DB_File", COUNT_FILE, O_RDWR | O_CREAT) {
my $fd = $db->fd;
open DBM, "+<&=$fd" or die "Could not dup DBM for lock: $!";
flock DBM, LOCK_EX;
undef $db;
$count{$url} = 0 unless exists $count{$url};
my $num_hits = ++$count{$url};
untie %count;
close DBM;
print "$num_hits\n";
} else {
print "[Error processing counter data]\n";
}