Help with retrieve a cookie

Discussion in 'Perl Misc' started by James, Nov 16, 2005.

  1. James

    James Guest

    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
    James, Nov 16, 2005
    #1
    1. Advertising

  2. James

    J. Gleixner Guest

    James wrote:
    > 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. Gleixner, Nov 16, 2005
    #2
    1. Advertising

  3. James

    James Guest

    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
    James, Nov 16, 2005
    #3
  4. James

    Keith Keller Guest

    On 2005-11-16, James <> wrote:
    > 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

    --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom
    see X- headers for PGP signature information
    Keith Keller, Nov 16, 2005
    #4
  5. James wrote:
    > 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).

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 16, 2005
    #5
  6. James

    Brian Wakem Guest

    Brian Wakem, Nov 16, 2005
    #6
  7. > 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

    --
    Lars Kellogg-Stedman <>
    This email address will expire on 2005-11-23.
    Lars Kellogg-Stedman, Nov 16, 2005
    #7
  8. On Wed, 16 Nov 2005, Lars Kellogg-Stedman wrote:

    > > 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.


    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.
    Alan J. Flavell, Nov 17, 2005
    #8
  9. > 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

    --
    Lars Kellogg-Stedman <>
    This email address will expire on 2005-11-23.
    Lars Kellogg-Stedman, Nov 17, 2005
    #9
  10. James

    Keith Keller Guest

    On 2005-11-17, Alan J. Flavell <> wrote:

    > 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

    --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom
    see X- headers for PGP signature information
    Keith Keller, Nov 17, 2005
    #10
  11. Keith Keller wrote:
    > On 2005-11-17, Alan J. Flavell <> wrote:
    >>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"?


    Parsing the cookie header?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 17, 2005
    #11
  12. James

    James Guest

    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
    James, Nov 17, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim
    Replies:
    2
    Views:
    380
    Peter O'Reilly
    Apr 5, 2004
  2. Jeff

    create and retrieve cookie?

    Jeff, Nov 21, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    416
    Mark Rae
    Nov 21, 2006
  3. Josh T
    Replies:
    2
    Views:
    649
    Alexey Smirnov
    Jun 9, 2007
  4. Replies:
    1
    Views:
    130
    Anthony Jones
    Mar 22, 2007
  5. Replies:
    5
    Views:
    148
    Scott Bryce
    Sep 11, 2005
Loading...

Share This Page