Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cookie problem

Status
Not open for further replies.

plasmacam

Programmer
Joined
Oct 27, 2006
Messages
5
Location
US
I am trying to set a cookie to track how manytimes a visitor has come to the site. I would like to set the cookie and work with it all on the same page. my code looks like the following:

my ($visits, $name, $C_visits, $msg);

#retrieve Name cookie
$visits = cookie('Visits');
$name = cookie('Name');
if ($visits < 0) {
$msg = "This is your first visit to our site!\n";
$visits = 1;
}
else {
$msg = "You have visited us $visits times.\n";
}
$visits++;
$C_visits = cookie(-name => "Visits",
-value => "$visits",
-expires => "+6M");
print header(-cookie => $C_visits);

It keeps giving me an error and have beat my head on the wall to try and figure it out. Please help.
 
I'm feeling that maybe the best solution for you is to read a little more of the documentation:


The one thing that I don't see in the above code is the initialization of your CGI object:

Code:
	use CGI;
	$query = new CGI;

If that doesn't solve your problem, then post the actual error that you're getting for us to be able to translate it's meaning for you.
 
Sorry I did not post eveything above, this is what is above:

#!/usr/bin/perl
use CGI qw(:standard);

#prevent Perl from creating undeclared variables
use strict;
 
this is the entire script:

#!/usr/bin/perl
#print "Content-type: text/html\n\n";
use CGI qw(:standard);

#prevent Perl from creating undeclared variables
use strict;

#declare variable
my ($visits, $name, $C_visits, $msg);

#retrieve Name cookie
$visits = cookie('Visits');
$name = cookie('Name');
if ($visits < 0) {
$msg = "This is your first visit to our site!\n";
$visits = 1;
}
else {
$msg = "You have visited us $visits times.\n";
}
$visits++;
$C_visits = cookie(-name => "Visits",
-value => "$visits",
-expires => "+6M");
print header(-cookie => $C_visits);

print "<HTML>\n";
print "<HEAD><TITLE>Jubilee Book Club</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H1>Jubilee Book Club Sign-In Form</H1><HR>\n";
print "<FORM \n";
print "ACTION=' \n";
print "METHOD=POST>\n";
print "<TABLE>\n";
print "<TR><TD>Name:</TD><TD>\n";
print "<INPUT TYPE=text NAME=Name SIZE=25> $msg\n";
print "</TD></TR>\n";
print "</TABLE>\n";
print "<BR><INPUT TYPE=submit VALUE=Submit>\n";
print "</FORM></BODY></HTML>\n";
 
Well, I'm going to once again suggest the same solution. Try creating an instance of CGI. I'm not certainly this is strictly necessary. But it's what the documentation does, so give it a try.

Code:
#prevent Perl from creating undeclared variables
use strict;

my $cgi = new CGI;
 
>> It keeps giving me an error

It would help to know what error you are getting.

- Kevin, perl coder unexceptional!
 
server 500 error. Doesnt make sence though as i just created a different cookie similar to this one and it works just fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top