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

Hi Monks,
I am trying to get the value of $ENV{'QUERY_STRING'} into a cookie but it seems that it doesn't work the way I have in my code, any help?
Here is what I have:

#! /perl/bin/perl use CGI; $query = new CGI; #print $query->header; #print $query->header(-cookie=>$cookie); #print $query->start_html('My cookie-get.cgi program'); #print $query->h3('The cookie is ...'); #Check if cookie is true else, creating a cookie... $theCookie = $query->cookie('aa_exit'); if(!$theCookie){ $exit_link = "$ENV{'QUERY_STRING'}"; $cookie = $query->cookie(-name=>'aa_exit', -value=>$exit_link, -expires=>'+4h', -path=>'/'); print $query->header(-cookie=>$cookie); print $query->h3('The cookie NOW is ...'); $theCookie = $query->cookie('aa_exit'); print "<BLOCKQUOTE>^^^^^\n"; print $theCookie; print "^^^^^</BLOCKQUOTE><br>\n"; }else{ print $query->header; print "<BLOCKQUOTE>*****\n"; print $theCookie; print "*****</BLOCKQUOTE>\n"; } print $query->end_html;


Thank you!

Replies are listed 'Best First'.
Re: Cookie Problem
by mda2 (Hermit) on Jun 10, 2005 at 16:17 UTC
    You are trying to get cookie after send it to browser. It's only available at next request, it accepted!

    Define your value with same of cookie set, or try to redirect, and check cookie avaliable.

    $exit_link = "$ENV{'QUERY_STRING'}"; $cookie = $query->cookie(-name=>'aa_exit', -value=>$exit_link, -expires=>'+4h', -path=>'/'); ... $theCookie = $query->cookie('aa_exit'); if(!$theCookie){ $exit_link = "$ENV{'QUERY_STRING'}"; # $theCookie = $query->cookie('aa_exit'); WRONG $theCookie = $ENV{'QUERY_STRING'}; ... }

    --
    Marco Antonio
    Rio-PM

Re: Cookie Problem
by omega_monk (Scribe) on Jun 10, 2005 at 16:00 UTC
    Not necessarily the solution, but you should be using
    use strict; # and this really helps DURING development. use CGI::Carp qw(fatalsToBrowser);
    I tried setting '$ENV{'QUERY_STRING'} = "test";' before the blockquote, and it printed as expected, so not sure yet why(my own ignorance, per usual, but the only way to learn) why $ENV{'QUERY_STRING'} is not being set...

    update: I took your original code, and swapped
    $exit_link = "$ENV{'QUERY_STRING'}"; # for $exit_link = "Perlmonks rocks.";
    after a refresh it pulls up properly. So I gather that you cannot view the cookie contents the same time that you put it, and $ENV{'QUERY_STRING'} is empty(which makes sense now because there was no REQUEST).

    update(2); Spelling...
Re: Cookie Problem
by Elijah (Hermit) on Jun 10, 2005 at 16:19 UTC
    Your code works fine as it is written but maybe this is not what you are trying to accomplish? As it is now the first time a user visits your site it sets a cookie. Now every subsequent time that user returns to the site the cookie value will stay the same and not change. Is this what you want? The cookie value will only expire 4 hours after being originally set. Not sure what you mean when you say it does not work. Be a little more specific on what you want this script to do.