CGI counter
CGI counter
(OP)
Hello everyone,
I'm having trouble getting this script working.
Current issue:
When running the script, I get "You have visited this page 1 time(s)." then when I refresh the page it continue showing "1" instead of incrementing up to "2".
Desired output:
When Launching the script a cookie is created and the counter start at 1. When the page is refreshed or visited again, the number increment up to 2, 3 and so on.
Can someone please help me out with this? Let me know if you need more info...
Here's my script:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);
use strict;
use CGI::Carp qw(fatalsToBrowser);
use warnings;
#declare variables
my ($C_visits, $counter, %counter);
#Read the cookie to a variable
%counter = (cookie('$counter'))? cookie('$counter'):'none';
#create cookie
$C_visits = cookie(-name => "MyCookie",
-value => "$counter",
-expires => "+3M");
if (exists $counter{$_}) {
$counter{$_}++;
} else {
$counter{$_} = 1;
}
#send cookie to browser
# print header(-cookie => $counter);
#create Web page
print "<HTML>\n";
print "<HEAD><TITLE>My Counter</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H1 ALIGN=left>Counter<BR></H1><HR>\n";
print "<ALIGN=left>You have visited this page $counter{$_} time\(s\).\n";
print "</BODY></HTML>\n";
I'm having trouble getting this script working.
Current issue:
When running the script, I get "You have visited this page 1 time(s)." then when I refresh the page it continue showing "1" instead of incrementing up to "2".
Desired output:
When Launching the script a cookie is created and the counter start at 1. When the page is refreshed or visited again, the number increment up to 2, 3 and so on.
Can someone please help me out with this? Let me know if you need more info...
Here's my script:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);
use strict;
use CGI::Carp qw(fatalsToBrowser);
use warnings;
#declare variables
my ($C_visits, $counter, %counter);
#Read the cookie to a variable
%counter = (cookie('$counter'))? cookie('$counter'):'none';
#create cookie
$C_visits = cookie(-name => "MyCookie",
-value => "$counter",
-expires => "+3M");
if (exists $counter{$_}) {
$counter{$_}++;
} else {
$counter{$_} = 1;
}
#send cookie to browser
# print header(-cookie => $counter);
#create Web page
print "<HTML>\n";
print "<HEAD><TITLE>My Counter</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H1 ALIGN=left>Counter<BR></H1><HR>\n";
print "<ALIGN=left>You have visited this page $counter{$_} time\(s\).\n";
print "</BODY></HTML>\n";
RE: CGI counter
The following will work for you:
CODE
use CGI qw(:standard -debug);
use CGI::Carp qw(fatalsToBrowser);
use strict;
use warnings;
# Read the cookie to a variable
my $counter = cookie('counter') || 0;
$counter++;
# create cookie
my $cookie = cookie(
-name => "counter",
-value => $counter,
-expires => "+3M"
);
# Header with cookie
print header(-cookie => $cookie);
# create Web page
print <<"END_HTML";
<HTML>
<HEAD><TITLE>My Counter</TITLE></HEAD>
<BODY>
<H1 ALIGN=left>Counter<BR></H1><HR>
<ALIGN=left>You have visited this page $counter time(s).
</BODY></HTML>
END_HTML
1;
__END__