Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

CGI counter resets for no obvious reason

by fraktalisman (Hermit)
on Jan 30, 2001 at 14:46 UTC ( [id://55222]=perlquestion: print w/replies, xml ) Need Help??

fraktalisman has asked for the wisdom of the Perl Monks concerning the following question:

I use a counter script that is being called by a Flash File (SWF).
The script writes the count to a file;
and if the parameter 'rubrik' is passed via CGI, it also writes to another file that counts the hits in that 'rubrik' (category) seperately.
Every 100 hits, a mail is sent to me.
A mail shall also be sent if there was an error.

The original script did no file locking, this one does. Still, for some reason the counter is reset to zero from time to time.
I get the notification every 100 hits, but I never get a mail stating an error.
After reading all docs and faqs related to CGI, I found no explanation. Here is the code:

#!/usr/bin/perl use CGI; use Fcntl qw(:DEFAULT :flock); $mailprog = '/usr/sbin/sendmail'; $recipient = 'info@active-websight.de'; $appname = 'playcounter'; $query = new CGI; $rubrik=$query->param('rubrik'); $countfile="gespielt-gesamt.txt"; $bereich="insgesamt"; &countIt; if ($rubrik ne "") { $countfile="gespielt--$rubrik.txt"; $bereich="in der Rubrik $rubrik"; &countIt; } exit(0); ############## sub countIt { sysopen(FH, "$countfile", O_RDWR | O_CREAT) or &fehler; # autoflush FH $ofh = select(FH); $| = 1; select ($ofh); flock(FH, LOCK_EX) or &fehler; # Dateizugriff anfordern $zahl = <FH> || 0; $zahl++; seek(FH, 0, 0) or &fehler; print FH $zahl, "\n" or &fehler; truncate(FH, tell(FH)) or &fehler; close(FH) or &fehler; if (($zahl/100)==int($zahl/100) and $zahl>99) { &nachricht;} } ################ sub nachricht { $betreff="$zahl Puzzles gespielt $bereich"; if ($fehler ne "") {$betreff="FEHLERMELDUNG !!!";} open(MAIL,"|$mailprog -t") or goto NOMAIL; print MAIL "To: $recipient\n"; print MAIL "From: info\@puzzleme.de ($appname)\n"; print MAIL "Subject: $betreff\n"; print MAIL "$appname meldet:\n\n"; print MAIL "$betreff.\n"; if ($fehler ne "") {print MAIL "$fehler\n";} close (MAIL); NOMAIL: } ################ sub fehler { $fehler="Fehler ($!) beim Schreiben von $countfile"; &nachricht; exit(0); } ################

Replies are listed 'Best First'.
Re: CGI counter resets for no obvious reason
by flocto (Pilgrim) on Jan 30, 2001 at 18:23 UTC
    I can't see the actual mistake, but here are some things you should always do and maybe that even fixes your problem:
    • Always use "use strict;"!! You might even want to use the "-w" tag.
    • Don't use global variables to pass data to subroutines. use something like that instead: &count ($cathegory);
    • Try to enter an "chomp ($zahl);" between reading it and the incement-operator
    • Maybe it helps for debuging to insert that line after "$zahl++;":
      if (!$zahl) { &fehler ('Counter is zero'); }
    If that doesn't help at all review your flush/flock code. I never lock counter files and it always works fine, so I don't thinks it's neccessary..
    Regards, octopus
      I never lock counter files and it always works fine, so I don't thinks it's neccessary.. *boggle*

      Personally, I never lock my house door and my stuff is always there when I get back so I don't think doors are necessary.

      Updated I didn't intend this to be mean but seriously, people haven't invented multiple ways to file lock just out of sheer boredom or for the excitement of it.

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

        Yea, file locking is very important nomatter how unimportant you think it is. I made a really simple counter a couple weeks ago and my friend and I tested it. As soon as we both accessed the counter at the same time the '1' went to a '2.46578837244004' or something like that. I think the counter was along the lines of:
        $cfile="counter.txt"; open(COUNT,$cfile); $count=<COUNT>; $count++; close(COUNT); open(COUNT,">$cfile"); print COUNT $count; close(COUNT); print "content-type: text/html\n\n"; print $count;
        (pretty confusing at first glance, eh?) Notice that there is no flock in there or any type of locking. Here are some links you should check out.
        Simple locking
        File locking
        flock
        Fcntl

        Wanna be perl hacker.
        Dave AKA damian

        I encourage you to email me
        okok, I correct myself: For my special purpose(s) filelocking wasn't necessary. Everybody who wants to write serious counter-applications (not just for fun) should lock his/her files and make backups every now and then..

        octopus
        --
        GED/CC d-- s:- a--- C++(+++) UL+++ P++++$ L++>++++ E--- W+++@ N o? K? w-- O- M-(+) V? !PS !PE !Y PGP+(++) t-- 5 X+ R+(+++) tv+(++) b++@ DI+() D+ G++ e->+++ h!++ r+(++) y+
Re: CGI counter resets for no obvious reason
by cianoz (Friar) on Jan 30, 2001 at 18:17 UTC
    this is a (stupid) counter i wrote for exercise years ago: maybe it could help you...
    !/usr/bin/perl -w use Fcntl qw(:flock); my $filename = '/tmp/counter'; if(-e $filename) { open FH, "+< $filename" || die "cannot open: $!"; } else { #first time... open FH, "+> $filename" || die "cannot open: $!"; } lock(FH); my $count = <FH> || 0; $count++; seek FH, 0,0; print FH $count; unlock(FH); print "$count\n"; sub lock { my $fh = shift; flock $fh, LOCK_EX; } sub unlock { my $fh = shift; flock $fh, LOCK_UN; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-29 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found