add counter to a form
Hello!
Got a question..
Is there a way to add an invisible counter to a form (like the alien form = af.cgi) so every time a user sends an email thru a form, it saves the quantity in a .log file and then the webmaster can check how many users sent that information.
Thanks
Nicolas Escobar J.
http://www.developy.com
Free resources for webmasters in spanish
japhy posted this at 14:56 — 29th November 2000.
They have: 161 posts
Joined: Dec 1999
There's no need to use the form to do it, just modify the program to increment a counter file every time the program is accessed (successfully, mind you -- do this after any error checking you need done).
use POSIX qw( O_RDWR O_CREAT LOCK_EX );
$counter = "/path/to/counter.txt";
sysopen COUNTER, $counter, O_RDWR|O_CREAT or die "can't read/write $counter: $!";
flock COUNTER, LOCK_EX or die "can't flock: $!";
chomp (my $n = <COUNTER> || 0);
seek COUNTER, 0, 0 or die "can't rewind: $!";
print COUNTER ++$n or die "can't print: $!";
truncate COUNTER or die "can't truncate: $!";
close COUNTER or die "can't close: $!"
Nico posted this at 16:20 — 29th November 2000.
They have: 96 posts
Joined: Feb 1999
Tks japhy
but I just added that code to the script and an error ocurred.
This is the script I am using
http://www.cgi.tj/scripts/alienform/af.txt
Thanks again,
Nicolas Escobar J.
http://www.developy.com
Free resources for webmasters in spanish
japhy posted this at 16:32 — 29th November 2000.
They have: 161 posts
Joined: Dec 1999
I'm sorry, there were syntax errors is the code I provided. I should have tried running it before pawning it off as working code.
use Fcntl qw( O_RDWR O_CREAT LOCK_EX );
$counter = "counter.txt";
sysopen COUNTER, $counter, O_RDWR|O_CREAT or die "can't read/write $counter: $!$
flock COUNTER, LOCK_EX or die "can't flock: $!";
chomp (my $n = <COUNTER> || 0);
seek COUNTER, 0, 0 or die "can't rewind: $!";
print COUNTER ++$n or die "can't print: $!";
truncate COUNTER, tell COUNTER or die "can't truncate: $!";
close COUNTER or die "can't close: $!";
This code works for me. And you might want to include the CGI::Carp module in your program, so that if this die()s, you'll be alerted:
use CGI::Carp 'fatalsToBrowser';
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.