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);
}
################
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.