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";
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.