in reply to A rotten cookie

I'm prepared for any downvoting for a request like this but at this point

What kind of community places views asking for an example of something as negative?

Can someone show me a very basic snippet for checking to see if a cookie exists, if it doesn't print a form and use whatever data they present from the form as the cookie?

Certainly, assuming you know how to print out HTML documents, Using CGI.pm you would do the following:

use CGI; my $q = new CGI; my $cookie = $q->cookie(-name=>'form_info') || ''; if ($cookie) { # you have the cookie, validate the info or whatever now } elsif ($q->param('form_info')) { # you've received the form info # now set the cookie my $cookie_to_send = $q->cookie(-name=>'form_info'); # print out header print $q->header(-cookie=>$cookie); # print out HTML header, then html document } else { # no cookie or form params received # print out HTML form }

Hope that helps :)

Replies are listed 'Best First'.
Re: Re: A rotten cookie
by sulfericacid (Deacon) on Aug 05, 2003 at 09:45 UTC
    Thank you! This is the closest I've been to getting anything to work. I can get to the 2nd loop (elsif) but for some reason it won't set the cookie and it won't process the if($cookie). I do a test print on $cookie to see what it contains and it never contains anything, why would that be?

    I got confused when you assigned $cookie_to_send to the cookie but printed to header the cookie instead of $cookie_to_send so I tried using both variations; neither errored and neither stored anything in the cookie. Any ideas why?

    Thanks so much!

    my $code = param('name'); my $cookie = cookie(-name=>'test') || ''; if ($cookie) { } elsif ($code) { my $cookie_to_send = cookie(-name=>'test' -value=>"$code", -expiration=>'+1h'); # print out header print header(-cookie=>$cookie_to_send); print "You had a cookie!<br>"; print "Code: $code<br>"; print "Cookie: $cookie"; } else { print header, start_html; print "You have no cookie"; print start_form( -action => '' ), table( Tr( td("Name:"), td( textfield( -name => 'name', -size => '15 +' ) ) ), Tr( td("Email:"), td( textfield( -name => 'email', -size => '15 +' ) ) ), Tr( td( submit('send') ), ), end_form, ); }


    "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

      i think your problem is here:

      } elsif ($code) { my $cookie_to_send = cookie(-name=>'test' -value=>"$code", -expiration=>'+1h'); # print out header print header(-cookie=>$cookie_to_send); print "You had a cookie!<br>"; print "Code: $code<br>"; print "Cookie: $cookie";

      "You had a cookie!" is false. they didn't have a cookie. that's why you just set one. you can't read the cookie you set until they visit the page (or another that checks for the cookie) again.

      anders pearson