Help with retrieve a cookie

J

James

Try to retrieve a cookie from a perl-cgi script, but gets an empty
string for the cookie.

----------------------------------------------------
#!/bin/env perl -w

use CGI qw:)standard);
print "Content-type: text/html\n\n";

my $cookie = cookie( -name => 'ID', -value => 'bob');

my $ID = cookie('ID');
print "ID = $ID";

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

web-page output:

ID =
Set-Cookie: ID=bob; path=/ Date: Wed, 16 Nov 2005 01:51:06 GMT
Content-Type: text/html; charset=ISO-8859-1

How do I retrieve a cookie?
Thanks in advance.


James
 
J

J. Gleixner

James said:
Try to retrieve a cookie from a perl-cgi script, but gets an empty
string for the cookie.

----------------------------------------------------
#!/bin/env perl -w

use CGI qw:)standard);
print "Content-type: text/html\n\n";

my $cookie = cookie( -name => 'ID', -value => 'bob');

my $ID = cookie('ID');
print "ID = $ID";

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

web-page output:

ID =
Set-Cookie: ID=bob; path=/ Date: Wed, 16 Nov 2005 01:51:06 GMT
Content-Type: text/html; charset=ISO-8859-1

How do I retrieve a cookie?


use CGI qw:)standard);
my $cookie = cookie( -name => 'ID', -value => 'bob');
print header(-cookie=>$cookie);

header() does the "Content-Type.." for you, and adding -cookie includes
the cookie information in the header.

The page won't contain any information, but the browser will
receive the cookie.
 
J

James

I don't understand.
Once I store the cookie, how do I retrieve the cookie in another cgi
script.
How do I display the cookie value?
Sample script might help.
Thanks.

James
 
K

Keith Keller

Once I store the cookie, how do I retrieve the cookie in another cgi
script.
How do I display the cookie value?
Sample script might help.

Sample code for both setting and retrieving cookies is in the
documentation for the CGI module. Have you read it yet?

--keith
 
G

Gunnar Hjalmarsson

James said:
I don't understand.
Once I store the cookie, how do I retrieve the cookie in another cgi
script.
How do I display the cookie value?

Sounds to me as if you don't have a Perl problem, but rather lack
understanding of how cookies work. Personally I have found this site
very useful: http://www.cookiecentral.com/ (especially the FAQ).
 
L

Lars Kellogg-Stedman

Try to retrieve a cookie from a perl-cgi script, but gets an empty
string for the cookie.

You have to send the cookie to a client (that supports cookies) before
you can retrieve it. There are multiple problems with this script:

(1) You're sending a header to the client that never sets a cookie:
use CGI qw:)standard);
print "Content-type: text/html\n\n";

Because you never send the Set-cookie header to the client (it's part
of your document content, not your header), the client never sees it.

(2) Even if you fix (1), the following doesn't make any sense:
my $cookie = cookie( -name => 'ID', -value => 'bob');
my $ID = cookie('ID');

Since you've just created the cookie, and haven't sent it to the
client yet, the cookie() method probably won't return anything useful.
It would make more sense to check the cookie() method first before you
try sending anything to the client (maybe the client already *has* the
cookie you're looking for).

-- Lars
 
A

Alan J. Flavell

You have to send the cookie to a client (that supports cookies)
before you can retrieve it.

Of course this isn't really a Perl question, but a question about how
the web works (part of the HTTP protocol, really). But ISTM that this
whole terminology of "retrieve a cookie" is an error in the thought
process. It's not as if the server script goes extending a tentacle
to the client, and grabbing the cookie out of its clutches...

If the client is disposed to provide the cookie(s) that it had
previously been sent, it will include it/them in its request to the
server, and the server will make them available in the environment of
the CGI request. All that the CGI script has to do is to read
it/them. No fetching and carrying by the script is involved. So, to
my way of thinking, calling it "retrieval" is bad terminology.
There are multiple problems with this script:

Indeed. But I feel sure that the underlying problem is a faulty
conceptual model of how it's meant to work.

cheers...

In the early days of cookies, I recall trying to access a web site and
apparently getting no response. I gave up and went home, unaware of
what was happening. Next morning I discovered what the server was
doing: it had sent a cookie and redirected to the same URL, expecting
to get the cookie back again. However, I had disabled the browser for
cookies, so it sent one again, and redirected to the same URL. This
had gone on and on for tens of thousands of redirections overnight,
and was still at it in the morning.
 
L

Lars Kellogg-Stedman

to get the cookie back again. However, I had disabled the browser for
cookies, so it sent one again, and redirected to the same URL. This
had gone on and on for tens of thousands of redirections overnight,
and was still at it in the morning.

I've managed to do this with CDW (online retailer) recently, even with
cookies enabled. I think it was just Firefox having a bad day.
Fortunately it also has a redirection limit, so it actually stops after
a few seconds.

-- Lars
 
K

Keith Keller

Of course this isn't really a Perl question, but a question about how
the web works (part of the HTTP protocol, really). But ISTM that this
whole terminology of "retrieve a cookie" is an error in the thought
process. It's not as if the server script goes extending a tentacle
to the client, and grabbing the cookie out of its clutches...

True, but the CGI.pm docs call it retrieving a cookie. I'm not
sure what an alternative would be: maybe "reading the value
of the cookie"?

--keith
 
G

Gunnar Hjalmarsson

Keith said:
True, but the CGI.pm docs call it retrieving a cookie. I'm not
sure what an alternative would be: maybe "reading the value
of the cookie"?

Parsing the cookie header?
 
J

James

Thanks to all, I managed to make it work.

use CGI;
my $q = new CGI;
my $c = $q->cookie( -name => 'ID', -value => 'bob');
print $q->header(-type => "text/html", -cookie => $c);
my $ID = $q->cookie('ID');

Thanks for the comments and useful links.

James
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top