Baz has asked for the wisdom of the Perl Monks concerning the following question:

Ok, the code below manages to create the cookie and its in my cookie folder.....but the call to
$query->cookie("MY_NAME");
returns NULL( so does $ENV(COOKIE),$ENV(HTTP_COOKIE)...whatever)

Clive made the following comment....
Only cookies sent from the browser are stored in the $ENV{'HTTP_COOKIE'} / $q->cookie() object, so if you've just set the cookie, it's not available in that object until the browser next calls the script.

I dont understand this fully ??, maybe someone might spell it out for me....can i not just create a cookie and then request its contents in the next line of the very same script?

Or maybe the $ENV(COOKIE) variable is not available on tripod( would they be that mean)...ive set up an account with prohosting but im still having diffiulty getting it to run a script(something to do with chmod i think)

anyway the script is named:
and the code is as follows
------------------------------------------------------- Code ------------------------------------------------------- #!/usr/local/bin/perl -w use strict; use CGI; my $query = new CGI; my $cookie_out = $query->cookie( -name=>"MY_NAME", -expires=>'+24h', -domain=>'cgi.tripod.com', -secure=>0 ); print $query->header(-cookie=>$cookie_out); print $query->start_html("Cookie Test"); # DEBUG TO SCREEN print $query->p; print $query->header(-cookie=>$cookie_out); print $query->p; my $cookie_in = $query->cookie("MY_NAME"); if($cookie_in) { print $cookie_in; } else { print "Can't find the cookie"; } print $query->p; print $query->end_html;

BROWSER DISPLAYS

Set-cookie: domain=cgi.tripod.com; expires=Monday, 27-Aug-2001 22:50:49 GMT Content-type: text/html

Can't find the cookie


COOKIE CONTAINS

0 782699264 29437766 42193056 29437564 *

Replies are listed 'Best First'.
Re: The Cookie Monster Returns......$ENV on tripod???????
by chromatic (Archbishop) on Aug 28, 2001 at 03:44 UTC
    can i not just create a cookie and then request its contents in the next line of the very same script?

    Exactly.

    The user agent (web browser) makes a request of the server. It has no cookie. It then waits for a response.

    The web server receives the request and fills in all of the environmental variables. Since the user agent had no cookie when it made the request, $ENV{COOKIE} is not set. By the time your program gets control, CGI.pm attempts to pull a cookie out of $ENV{COOKIE}. The user agent still has no cookie. CGI.pm finds no cookie. Your program gets no cookie.

    Your program then creates a cookie and sends information to the user agent.

    The user agent will only send the cookie at its next request. You don't get an instantaneous reaction with this sort of programming. The client sends a chunk, then the server sends a chunk.

    Make sense?

      Thanx guys, Ive moved my account to prohosting now I tried my code there and the same thing is still happening i.e. cookie created by not able to read it back

      After I had created the cookie I edited my script so that it now only requested the cookie instead of creating it( the cookie is already there and it should therefore by sent to the script by the user agent) I searched for the cookie with

      my $cookie_in = $query->cookie("MY_NAME");

      but still nothing when I tried Andy's code I got the following....

      pkit_id=user_id&nirvana7&hash&965e21f712b38aa13586765230ccc75a
      pkit_session_id=c0fcc76283804096c4c56ca15e9eb4af

      THis is the code he used to retrieve his cookie
      my $rcvd_cookies = $ENV{'HTTP_COOKIE'}; my @cookies = split /;/, $rcvd_cookies; foreach my $cookie (@cookies) { print $cookie,"\n"; }
      Therefore the raw data of this return, has the two lines above seperated by a semicolon

      Im now initialising the cookie with....
      my $cookie_out = $query->cookie( -name=>"MY_NAME", -value=>"Barry Griffin", -expires=>'+24h', -path=>'/cgi-bin/~nirvana7', -domain=>'rain.prohosting.com', -secure=>0 );
      The cookie content is

      MY_NAME Barry%20Griffin rain.prohosting.com/cgi-bin/~nirvana7 0 1510090368 29437992 743584160 29437790 *

      MY Script is located at:
      http://rain.prohosting.com/~nirvana7/cgi-bin/dumb1.cgi
Re: The Cookie Monster Returns......$ENV on tripod???????
by bladx (Chaplain) on Aug 28, 2001 at 03:23 UTC
    Ok, Baz

    I did some reviewing on how to do this in CGI, since I haven't done anything with CGI for a while, and came up with *possibly* a solution to your problem. Note: I am not sure if it is exactly what you want it to do, but at least it finds the cookie, and prints its value :).

    #!/usr/local/bin/perl -w use strict; use CGI; my $query = new CGI; my $cookie_out = $query->cookie( -name=>"MY_NAME", -value=>"Barry Griffin", -expires=>'+24h', -path=>'/cgi-bin/', -domain=>'bladx.perlmonk.org', -secure=>0 ); print $query->header(-cookie=>$cookie_out); print $query->start_html("Cookie Test"); # DEBUG TO SCREEN my $rcvd_cookies = $ENV{'HTTP_COOKIE'}; my @cookies = split /;/, $rcvd_cookies; foreach my $cookie (@cookies) { print $cookie,"\n"; } print $query->end_html;

    Hopefully this helps with your cookies fun. And a little note, I know that you are still somewhat new to asking question at pm.org, and just to let you know, you may want to make shorter, more descriptive titles :) ... just a thought, keep on the Perl path though!

    Also, if you would like to see where I tested this script, I did it at: here. I don't think Tripod is the best server, I still suggest talking to jcwren about getting a free testing account at perlmonk.org!

    Andy Summers