Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

reading and writing a CGI cookie in the same page

by ibanix (Hermit)
on Feb 21, 2003 at 01:49 UTC ( [id://237302]=perlquestion: print w/replies, xml ) Need Help??

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

Hi fellow monks,

I've got a CGI app in development. I'm trying to set a cookie, and then immediately read from it. I don't seem to have any problems setting the cookie, but I can't seem to read right back from it. Code looks something like this:
$new_cookie = cookie_creator_sub() if (some condition); .... if ($new_cookie) { print $q->header(-cookie=>$new_cookie); } else { print $q->header(); } my %cookie = $q->cookie('info'); .... sub cookie_creator_sub { ..... my %cookie_info = ( 'tempfile' => $tempfile, 'timestamp' => $timestamp, ); $newcookie = $q->cookie(-name=>'info', -value=>\%cookie_info, ); return $newcookie; }
If I put debug info in, I can see that the cookie is being created, but not read; on the next refresh or call of the script the cookie is read.

Am I doing something wrong, or can't you create and read a cookie in the same pageview?

Thanks~!

$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Replies are listed 'Best First'.
Re: reading and writing a CGI cookie in the same page
by VSarkiss (Monsignor) on Feb 21, 2003 at 02:02 UTC

    can't you create and read a cookie in the same pageview?
    No, you can't.

    The first time your CGI is called to gen up the page, the cookie is sent, so it's not available until after the browser (or whichever user agent) has retrieved the page and saved the cookie (if it chose to). Hence it's not available until the same agent makes the request again -- which is when your CGI is called the second time, and you see the cookie.

Re: reading and writing a CGI cookie in the same page
by shotgunefx (Parson) on Feb 21, 2003 at 02:07 UTC
    Creating the cookie does not update the object.
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use CGI; my $q = CGI->new(); my $cookie = $q->cookie(-name=>'info',-value=>'bar'); print $q->header(-cookie=>$cookie); print $q->cookie('info'),"\n"; print Dumper($q); __OUTPUT__ Set-Cookie: info=bar; path=/ Date: Fri, 21 Feb 2003 01:48:32 GMT Content-Type: text/html; charset=ISO-8859-1 $VAR1 = bless( { '.header_printed' => '1', '.charset' => 'ISO-8859-1', '.parameters' => [], '.fieldnames' => {}, '.cookies' => undef }, 'CGI' );
    Either use an assigned variable to get it's value or subclass CGI (just need to overide cookie() to mitigate the problem.)

    -Lee

    "To be civilized is to deny one's nature."
Re: reading and writing a CGI cookie in the same page
by dws (Chancellor) on Feb 21, 2003 at 02:52 UTC
    You've already gotten an answer to the question you posed, so I'll add a minor coding suggestion.

    You can eliminate a conditional by writing

    push @new_cookies, cookie_creator_sub() if <<some condition>>; ... $q->header(-cookie => [ @new_cookies ]);
    If @new_cookies is empty, no cookies are added. This approach gives the flexibility of adding additonal cookies without having to modify existing code.

Re: reading and writing a CGI cookie in the same page
by Cabrion (Friar) on Feb 21, 2003 at 02:08 UTC
    can't you create and read a cookie in the same pageview?

    Not without some javascript trickery, or a redirect to yourself.

      Forget javascript trickery (as a "helper" javascript rocks, but you can't rely on it so don't rely on it from the beginning).

      It is best to do a redirect (some might call it standard practice). See merlyns article on Basic Cookie Management.

      Basic strategy is

      • user visits foo.cgi
      • you try to set a cookie whilst redirecting to foo.cgi?redirect=1
      • if you can read cookie, do your thing, if not, user don't support cookies, so don't redirect again (redirect=1 to prevent an infinite loop)


      MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
      I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
      ** The Third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://237302]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-04-18 10:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found