I've never written a CGI script that uses cookies before and I'm having trouble with my first attempt. I tested with Netscape and had Netscape ask me before accepting any cookies. Here's my setting code:
my $cookie = $cgi->cookie( -name=>'botLogin', -value=>"$username:$trueName:$Hash"); print $cgi->header(-cookie=>[$cookie,$cookie]),$cgi->start_html(-title +=>'Login');
Then I tried to retrieve the cookie using a variety of different methods:
#first method..this attempts to just get anything out there and sh +ow it to me print $cgi->h3("First cookie retrieval method..."); my %cookies = parse CGI::Cookie($ENV{COOKIE}); foreach (keys %cookies) { print $cgi->h2("START $_ => $cookies{$_}"); } print $cgi->h3("End first method."); #second method...like the first, doesn't look for a particular coo +kie print $cgi->h3("Second retrieval method..."); %cookies = fetch CGI::Cookie; foreach (keys %cookies) { print $cgi->h3("START $_ => $cookies{$_}"); } print $cgi->h3("End second method"); #this was my original method...tries to get only the cookie that I + set #then parses it's value to retrieve the data I wanted from it if(defined(my $cookieHolder = $cgi->cookie(-name=>'botLogin'))){ print $cgi->h3("Retrieved cookie named botLoginName: $cookieHo +lder"); } else{ print $cgi->h3("No cookie retrieved."); }
Using Netscape (version 4.77, or IE 5.5 for that matter), I ran the setting code. Sure enough, the browser asked me if I wanted to accept the cookie. The name and value were right. I accepted. Then I tried to retrieve it--no dice. Eventually I discovered that I had to run the setting script twice in a row before I was able to retreive the cookies. What am I doing wrong? Why aren't my cookies taking the first time?

In reply to 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.