in reply to Re: simple web counter
in thread simple web counter

Even i tried this code from the same book . Following is my code
#!/usr/bin/perl use strict; use Fcntl qw (LOCK_EX); use Fcntl; use DB_File; use constant COUNT_FILE => "count.dbm"; my %count; my $num_hits; my $url = $ENV{REQUEST_URI}; local *DBM; print "Content-type : text/plain\n\n"; my $db = tie %count , "DB_File" , COUNT_FILE , O_RDWR | O_CREAT; if( $db ) { my $fd = $db->fd; open DBM , "+<&=$fd" or die "Count not dump DBM for lock: $!"; flock DBM,LOCK_EX; undef $db; $count{$url} = 0 unless exists $count{$url}; $num_hits = ++$count{$url}; untie %count; close DBM; print "$num_hits\n"; }else{ print "[Error processing counter data]\n"; }
Following are my observations

when run the code from my terminal it updates the count and prints

root@inll11325816l:/usr/lib/cgi-bin# perl counter.cgi Content-type : text/plain 11 root@inll11325816l:/usr/lib/cgi-bin# vi counter.cgi root@inll11325816l:/usr/lib/cgi-bin# perl counter.cgi Content-type : text/plain 12 root@inll11325816l:/usr/lib/cgi-bin# vi counter.cgi root@inll11325816l:/usr/lib/cgi-bin# perl counter.cgi Content-type : text/plain 13 root@inll11325816l:/usr/lib/cgi-bin# perl counter.cgi Content-type : text/plain 14

When i run from the Browser it just exits from the if loop and prints the statement in the else block I have another question also here , why there is no check for the URI we are accessing here , wht if the DOCUMENT_URI or REQUEST_URI ( in my case has a different .html file that is been accessed Can anyone please help here ?

Replies are listed 'Best First'.
Re^3: simple web counter
by poj (Abbot) on Mar 18, 2017 at 14:23 UTC

    Add absolute /path/folder/ that is writeable by the webserver. For example /tmp/

    use constant COUNT_FILE => "/tmp/count.dbm";
    poj