urgent problem: can't retrieve cookie

D

deryk_deguzman

hi. i've spent 5 hours trying to solve this problem but can't seem to
handle it.

my code to set a cookie goes like this:

use CGI;
$qs = new CGI;
$ckName = cookie(-name=>'name', -value=>'aaa');
print $qs->header(-cookie=>$ckName);

then i have a link on that page that goes to a cgi file that has:

use CGI;
$query = new CGI;
print $query->header;
print $query->start_html();
$cookie = $query->cookie('name');
print "IT IS: $cookie";

my problem is that the second file isn't getting the cookie. it's in
the same directory as my first cgi file but can't find the cookie. what
can i do to fix this?

also, when i print out $ckName in my first file, it prints out
"name=blah; path=/" to my page. but when i do something like:

$mycookie = $qs->cookie('name');
print $mycookie;

nothing comes out.

i'd really appreciate any help i can get with this. thanks.
 
D

deryk_deguzman

btw,

i also tried setting the cookie path to '/cgi-bin', which is where the
2 cgi files are, but that didn't work either.
 
S

Scott Bryce

my code to set a cookie goes like this:

use CGI;
$qs = new CGI;
$ckName = cookie(-name=>'name', -value=>'aaa');
print $qs->header(-cookie=>$ckName);

Please include use strict; and use warnings; in your scripts. Had you
done so, Perl would have told you where the problem was.

use strict;
use warnings;
use CGI;

my $qs = new CGI;
my $ckName = $qs->cookie(-name=>'name', -value=>'aaa');
#------------^^^^^
print $qs->header(-cookie=>$ckName);
then i have a link on that page that goes to a cgi file that has:

use CGI;
$query = new CGI;
print $query->header;
print $query->start_html();
$cookie = $query->cookie('name');
print "IT IS: $cookie";

use strict;
use warnings;
use CGI;

my $query = new CGI;
print $query->header;
print $query->start_html();
my $cookie = $query->cookie('name');
print "IT IS: $cookie";
my problem is that the second file isn't getting the cookie.

Your problem was that the first script was not setting the cookie.
 
S

Scott Bryce

Scott said:
Please include use strict; and use warnings; in your scripts. Had you
done so, Perl would have told you where the problem was.

I'll correct myself here. Had you tried to compile and run the script,
even without strictures and warnings, Perl would have told you where the
problem is. Is this really your code?
 
D

deryk_deguzman

yes, it is. and thanks for the reply. it appears that the cookie is
set. when i try to print the header twice, i see the header as printing
"Set-cookie:name='aaa'...".

how would u advise i solve this problem of not setting the cookie
correctly? i've compared this code with other scripts out there, and it
seems to be ok.
 
S

Scott Bryce

Please quote enough of the post you are responding to to put your
response in context.


yes, it is.

I assume you mean that the code you posted is your actual code. It can't
be. It doesn't compile.
it appears that the cookie is set.

How do you know that? Have you asked your browser whether IT thinks the
cookie was set?
when i try to print the header twice, i see the header as printing
"Set-cookie:name='aaa'...".

This does not mean that the cookie is being set. When you send the
header the second time, the browser thinks it is part of your HTML
stream, which it actually is.
how would u advise i solve this problem of not setting the cookie
correctly?

The code I posted works on my machine. I would look for the problem
elsewhere.

i've compared this code with other scripts out there, and it seems to
be ok.

"Other scripts out there" are not necessarily a good way to tell if your
script is OK. If the "other scripts out there" don't use strictures or
warnings, they are suspect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top