Home > Archive > PERL CGI Beginners > March 2005 > CGI::Cookie Reading.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
CGI::Cookie Reading.
|
|
|
| ###################### 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.
| |
| Lawrence Statton 2005-03-29, 3:55 pm |
| > ########################## READ-COOKIE.CGI ###########################
>
> #!/usr/bin/perl
> use CGI;
> use CGI::Cookie;
> use CGI::Carp qw(fatalsToBrowser);
> use strict;
> use warnings;
>
> my %cookies =3D fetch CGI::Cookie;
> my $cookies;
> my $id =3D $cookies{'ID'}->value;
>
> 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.
>
>
check to see that the cookie exists before extracting its value
my $id = 'no cookie presented';
if ( exists $cookies{ID} ) {
$id = $cookies{ID}->value;
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
sort them into the correct order.
| |
| Randal L. Schwartz 2005-03-29, 8:55 pm |
| >>>>> "Sara" == Sara <sara_samsara@hotpop.com> writes:
Sara> IF the cookie already exists the script reads the value
Sara> perfectly, but if cookie doesn't exist or I mean for the first
Sara> time visitor, it gives error: "Can't call method "value" on an
Sara> undefined value"
Ahh, the classic cookie misunderstanding.
You can't *read* a cookie that hasn't been sent to you by a browser
yet. And the browser isn't going to send you a cookie until the
*second* hit, because the cookie is being set in the *first* hit.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
|
|
|
|
|