Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: simple web counter

by extremely (Priest)
on Sep 15, 2000 at 08:17 UTC ( [id://32622]=note: print w/replies, xml ) Need Help??


in reply to simple web counter

I don't know why it doesn't work but I can give you a few things to test.

Change the following line so it has a "4" where the "0" is. Does it always return 5 now? That would smell like a DB problem. Probably permissions. $count{$url} = 0 unless exists $count{$url};

Also, are you sure that your ENVironment contains DOCUMENT_URI? Always having a "" for that could be a problem.

Minor nit, why is "use strict;" turned off? What error made you do that? Is that or any other error in your webserver logs?

--
$you = new YOU;
honk() if $you->love(perl)

Replies are listed 'Best First'.
Re^2: simple web counter
by Anonymous Monk on Mar 18, 2017 at 13:56 UTC
    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 ?

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

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://32622]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (9)
As of 2024-04-18 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found