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

Before I continue, I must say I'm very prepared to get downvoted if it means someone will explain this to me.

Bart helped me get a cookie script to work earlier, or atleast I think it kind of works anyway. It is from the Perl Cookbook, page 694.

If you write something in and click enter, it 'says' it writes a cookie. I understand that, but it says +2y. When I click in my address bar and click enter again on script.pl, I have to add another choice in. Why isn't it keeping my data if it's supposed to be there for 2 years AND it's the same browser window?

Isn't it supposed to remember what I wrote for 2 years as long as it's the same window? What I'm trying to do is make it so I can login on script.pl and access script.pl?other= and still be logged in. Can someone explain why this isn't working as I expect it should be?

#!/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'); 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; } 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'."); if ($pass) { print "Still 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

Replies are listed 'Best First'.
Re: Cookie time again *eats cookie*
by castaway (Parson) on Dec 27, 2003 at 19:43 UTC
    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.

      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.

Re: Cookie time again *eats cookie*
by kal (Hermit) on Dec 27, 2003 at 20:56 UTC

    Hi sulfericacid,

    Others have attempted to show you problems with the script, so I won't bother. But, I do think you are having problems with what cookies actually are - for example, you say "I know cookies are gone the minute the window is closed": well, no, not quite. Some are, some aren't. If you set one to last two years, it should last that long come what may.

    What browser do you use? If you have something like Mozilla or Epiphany, you can actually look at the cookies themselves quite easily. This will allow you to actually check whether or not the cookie is being set. This is quite important - you could have two errors in your script. You're not going to read back the cookie if you don't manage to set it, and it you're both not setting it correct nor reading it back correctly, then you will mess around for ages and probably not get anywhere.

    Always best to try to narrow the problem down to as small an area as possible. If you can check the cookie is being set, then you know you have a problem reading the cookie back. If it's not, you know you're not setting it.

Re: Cookie time again *eats cookie*
by !1 (Hermit) on Dec 27, 2003 at 19:51 UTC

    Ok. I'm assuming you already know that the only value that this script will allow on the cookie is test. Since you are using CGI.pm's textfield method, it gets the values that were submitted. If you prefer to use your default value, you must use -override or -force. In other words, change your textfield call to:

    textfield(-name=>"flavor",-default=>$tasty,-override=>1)

    As for this pass bit, you need to edit your copy of CGI.pm. In CGI 3.01, on line 556 there's a comment about cake and eating it too. This will instruct you to uncomment line 559. Once you do that, param() can read parameters submitted from both the query string and STDIN. Oh yeah, you also submit the parameter as who, not pass. The value should be pass. I hope this helps.

    UPDATE: Realized you used url_param instead of param thus negating the need to edit CGI.pm. However remember the order of verification: check the cookie, failing that check params, failing that display the default page.

Re: Cookie time again *eats cookie*
by pg (Canon) on Dec 27, 2003 at 19:38 UTC

    So you plan to have your computer on for two years without interruption ;-)

    "The cookie will be saved and returned to your script until this expiration date is reached if the user exits the browser and restarts it."

    This is copied and pasted from CGI doc, which should actually read:

    "The cookie will be saved and returned to your script until this expiration date is reached ,or if the user exits the browser and restarts it."

    Hope it is now more clear to you. You are ealing with one type of cookie, called session cookie.

    Update:

    castaway's answer is much better!

      "The cookie will be saved and returned to your script until this expiration date is reached ,or if the user exits the browser and restarts it."
      That is so untrue. Under normal circumstances, the browser saves the cookie to disk, and it will be used until the expiration date has passed — unless the user deletes it, of course. Restarting the browser, or the computer, won't make a difference.

      If you want a cookie to last only until the browser is closed, don't provide an expiration date.

      p.s. An expiration date in the past will delete the cookie.

      That really wasn't the question, I know cookies are gone the minute the window is closed, but I don't see why they're gone when it's in the same browser window. All I'm doing is clicking ENTER in the location bar after I supposively have a cookie, and it's not sticking.


      "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