I need to keep track of the number of times a user visits a website. I have the code done but I don't think I am counting the correct way as the dynamic web page keeps showing the same number of visits. Any tips would be so helpful!

#!/usr/bin/perl
use CGI qw(:standard -debug);
use CGI::Carp qw(fatalsToBrowser);

#prevent Perl from creating undeclared variables
use strict;
 
#declare variables
my ($count, $C_record);

#assign input to variable
$count = cookie('count');

$count++;
 
 
#create cookie
$C_record = cookie(-name => "count",
                 -value => "$count ",
				 -path => "/cgi-bin/chap11",
                 -expires => "6M");
 
 
#send cookie to browser
print header(-cookie => $C_record);

#create Web page
print "<HTML>\n";
print "<HEAD><TITLE>Jubilee Book Club</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H1 ALIGN=center>Hello!<BR>\n";
print "You have been here $count times.</H1>\n";
print "</BODY></HTML>\n";

The script doesn't appear to read the cookie. In your error log (or whatever file STDERR writes to) you may find something like "Use of uninitialized value $count in concatenation (.) or string at..."

Sorry for the confusion. The error message I posted was caused by a print statement I inserted to print the value of $count before incrementing it. The following modified version of your script seems to work for me now. I'm not sure why.

#!/usr/bin/perl
#countvisits.cgi
use strict;
use warnings;

use CGI qw(:standard -debug);
use CGI::Carp qw(fatalsToBrowser);

#declare variables
my ($count, $C_record);

#Create a new CGI object
my $cgi = new CGI;

#Read the cookie
#assign input to variable
$count=$cgi->cookie('count');

$count++;
  
#create cookie
$C_record = cookie(-name => "count",
                 -value => $count,
                 -expires => "6M");
 
 
#send cookie to browser
print header(-cookie => $C_record);

#create Web page
print "<HTML>\n";
print "<HEAD><TITLE>Jubilee Book Club</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H1 ALIGN=center>Hello!<BR>\n";
print "You have been here $count times.</H1>\n";
print "</BODY></HTML>\n";
This article has been dead for over six months. Start a new discussion instead.