Try to follow the logic of your program. You are reading the value of the "favourite ice cream" cookie, and storing it in $tasty, but you don't check it anywhere. The first part of the script says 'if there is no value in the "flavor" parameter (stored in $favourite), then print normal headers, and the "please select a flavor" form.' If you call the script with no parameters, this is what it is going to do.

What you need to do is to check the value of the cookie before you check the parameters. ie:

#!/usr/bin/perl use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); my $cookiename = "favorite ice cream"; my $favorite = param('flavor'); my $tasty = cookie($cookiename); my $pass = url_param('pass'); # First check if we saved a cookie last time if($tasty) { print header(), start_html("Ice Cookies, #2"), h1("Hello Ice Cream"); print "You have already chosen a favorite!"; print("You have chose as your favorite flavor '$tasty'."); print end_html(); exit; } # No cookie, so if no favourite value, print new form unless ($favorite eq "test") { print header(), start_html("Ice Cookies"), h1("Hello Ice Cream"), hr(), start_form(), p("Please select a flavor: ", textfield("flavor",$tasty)), end_form(), hr(); exit; } # Favourite value was 'test', save cookie to browser my $cookie = cookie( -NAME => $cookiename, -VALUE => $favorite, -PATH => "/", -EXPIRES => "+2y", ); print header(-COOKIE => $cookie), start_html("Ice Cookies, #2"), h1("Hello Ice Cream"); print "You have already chosen a favorite!"; print "<a href=\"example.pl?who=pass\">new window</a>"; #p("You have chose as your favorite flavor '$favorite'."); # No idea what this is for.. if ($pass) { print "Still logged in"; }
The code gets executed from top to bottom..

C.


In reply to Re: Cookie time again *eats cookie* by castaway
in thread Cookie time again *eats cookie* by sulfericacid

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.