in reply to Cookie time again *eats cookie*

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.

Replies are listed 'Best First'.
Re: Re: Cookie time again *eats cookie*
by sulfericacid (Deacon) on Dec 27, 2003 at 20:02 UTC
    I've tested your code but it does the same thing as mine. It looks like it works, but if you go back to the url it forces you to use the form again.

    The if ($pass) {.. is to test if I have a url_param. I need this to work them and still keep you logged in.



    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid
      After trying it again, I assume that your browser is not set to check if documents have changed, unless the document says it has expired. So try setting the 'check documents' in your browser to 'always', or add a '-expires' => 'now' to the header() calls in your CGI. so that the browser will always check.

      C.