###################### SET-COOKIE.cgi ######################

#!/usr/bin/perl
use CGI qw/:standard/;
use CGI::Cookie;
use CGI::Carp qw(fatalsToBrowser);
use strict;
use warnings;

my $c = new CGI::Cookie(-name => 'ID',
-value => '987654321',
-domain => '.domain.com',
-expires => '+3M',
-path => '/'
);

my $q = new CGI;

print $q->header(-cookie=>$c);

print "ID cookie is set for 3 months";

######################################################################

The above code is working fine and sets up the cookie as required.

I have problems reading the cookie as follows:

########################## READ-COOKIE.CGI ###########################

#!/usr/bin/perl
use CGI;
use CGI::Cookie;
use CGI::Carp qw(fatalsToBrowser);
use strict;
use warnings;

my %cookies = fetch CGI::Cookie;
my $cookies;
my $id = $cookies{'ID'}->value;

my $q = new CGI;

print $q->header(-cookie=>$cookies);

print "Cookie ID = $id";
print "<br>Thanks\n";
############################################################

IF the cookie already exists the script reads the value perfectly, but if 
cookie doesn't exist or I mean for the first time visitor, it gives error: 
"Can't call method "value" on an undefined value"

I tried to do something like it: if ($id) {do blah} else {do blah} but that's 
not working either.

It's my first time using CGI::Cookie, how can I overcome this problem

Thanks for any input,

Sara.




Reply via email to