If you are reading it in the same script that you set it in (and on that run - it will be available if the script is called again), it will not contain the cookie you set.

Nothing wrong with your initial method of retrieval, as far as i can see.

Look at your cookie header code. any reason why you are setting $cookie twice? See docs - looks like you mis-copied their example that set two cookies.

But in answer to *why* it's not there, you need to look at the cgi.pm module itself. When retrieving cookies, cgi calls CGI::Cookie->fetch, when setting, it returns a cookie object created by CGI::Cookie(@param).

Don't make the mistake of thinking of the $cgi->cookie call as being an object - it's a method, context depending on how it's called.

If you wanted it to work as you expect, you'd need to fix things a little. I suggest you create a %cookie hash and add name value pairs to it and then use an array to send to CGI. EG,

#!/usr/bin/perl use strict; use CGI; use CGI::Carp 'fatalsToBrowser'; my $cgi = new CGI; # grab existing %cookie my %cookie; for ($cgi->cookie()) { $cookie{$_} = $cgi->cookie($_); } # set a couple of cookies; $cookie{'botLoginName'} = "XXXXX:YYYYY:ZZZZZ"; $cookie{'Other_cookie'} = 'whatever'; # create cookies for CGI my @cookies; for (keys %cookie) { push @cookies, $cgi->cookie( -name => $_, -value => $cookie{$_} ); } # print amended header print $cgi->header(-cookie=>[@cookies]),$cgi->start_html(-title=>'Logi +n'); # use our hash to check "cookie" if(defined(my $cookieHolder = $cookie{'botLoginName'})){ print $cgi->h3("Retrieved cookie named botLoginName: $cookieHo +lder"); } else{ print $cgi->h3("No cookie retrieved."); } # display all stored print $cgi->h4('Stored Cookies'); for (keys %cookie) { print "$_ = $cookie{$_}".$cgi->br; }

But, having said that, it's probably better to write your code so that on first run (or run where username is sent) that you just check the param() rather than cookie()

HTH

cLive ;-)


In reply to Re: Cookies Not Getting Set First Time by cLive ;-)
in thread Cookies Not Getting Set First Time by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.