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

I've not come for ideas on this one but instead a sample source code of what I'm trying to accomplish. I'm prepared for any downvoting for a request like this but at this point, I'm not getting any further and I need major help or explanation.

I don't understand how you need to set a cookie with the headers..I don't understand how you're supposed to collect user data before you can print headers.

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?

All the codes I've seen checked for a cookie and if it didn't exist, they made a cookie with whatever information they want but it didn't require user data.

I don't quite understand the logic of how we're supposed to use specific data as cookie values before you're allowed to print anything. Thank you everyone for your support on this..

"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: A rotten cookie
by jsprat (Curate) on Aug 05, 2003 at 04:55 UTC
    Merlyn wrote a very good column on Basic cookie management. Look at lines 8 through 30 for the example, but be sure to read the whole article. Basically, it checks if the cookie exists, and if not it prints and posts the form, sets the cookie, then redirects to the same page.

    Hope this helps

Re: A rotten cookie
by bobn (Chaplain) on Aug 05, 2003 at 05:13 UTC

    Probably not up to merlyn's standards (lack of use strict for example), but her's something that works.

    First time you run it as a cgi, it gives you a form to fill in. After you fill in and and submit, it comes back with "Your cookie is". If you look at your browser's cookies at this point, you'll see it. The third time you run it, it prints as above, but this time because it saw the cookie come back, the value you originally entered shows up.

    #!/usr/bin/perl -Tw use CGI; use CGI::Cookie; my $q = CGI->new(); if ( $q->cookie('id') ) # cookie sent back by browser { print $q->header(-cookie=>$q->cookie('id')), $q->start_html, $q->h1('your cookie is ' . $q->cookie('id' +)), $q->end_html; } elsif ( $q->param('name') ) # form filled in by user { $c = CGI::Cookie->new(-name=>'id', -value=>$q->param('name') ); print $q->header(-cookie=>$c), $q->start_html, $q->h1('your cookie is ' . $q->cookie('id')), $q->end_html; } else # no cookie, nor form filled in { print $q->header(), $q->start_html, $q->start_form, "What's your name? ", $q->textfield('name'), $q->submit, $ +q->end_form, $q->end_html; }

    --Bob Niederman, http://bob-n.com
Re: A rotten cookie
by Anonymous Monk on Aug 05, 2003 at 05:22 UTC
    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 :)

      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

Re: A rotten cookie
by antirice (Priest) on Aug 05, 2003 at 15:29 UTC

    Kid tested. Mother approved.

    #!/usr/bin/perl -w use strict; use CGI qw/header cookie start_html end_html url param/; use Digest::MD5 qw(md5_hex); my %login = ( test => 'test' ); # this is just for demo $login{$_} = md5_hex($login{$_}) foreach (keys %login); sub checkSessionID { my $check = shift; my ($user,$pass) = split /:/,$check,2; return $login{$user} eq $pass ? 1:0; } sub checkUserPass { my $user = shift; my $pass = md5_hex(+shift); return $login{$user} eq $pass ? 1:0; } sub makeCookieValue { my $user = shift; my $pass = md5_hex(+shift); return "$user:$pass"; } my %options; my $cookie; # yes, dirty and disgusting...but...oh well if (param('clearCookie')) { $options{"-cookie"} = cookie(-name => 'sessionID', -value => 'bye', -expires => '-6M' # just in case ); } elsif (cookie('sessionID') && checkSessionID(cookie('sessionID'))) { $cookie = cookie(-name => 'sessionID', -value => cookie('sessionID'), -expires => '+1h' ); } elsif (param('username') && param('password') && checkUserPass(param(' +username'),param('password'))) { $cookie = cookie(-name => 'sessionID', -value => makeCookieValue(param('username'),param +('password')), -expires => '+1h' ); } $options{"-cookie"} = $cookie if $cookie; # if you wish, you can put more header options in %options # if you wish to put more cookies, do: # $options{'-cookie'} = [ $options{'-cookie'} ]; # push @$options{'-cookie'}, $_ foreach (@more_cookies); print header(%options),start_html; # $cookie acts as our switch if ($cookie) { print "Logged in and working! Cookie: ",cookie('sessionID') ? cookie +('sessionID'):"You just logged in!"; print "<br /><form action='",url(-absolute=>1),"' method='post'><inp +ut type='hidden' name='clearCookie' value='1'> <input type='Submit'></form>"; } else { print "<form action='",url(-absolute=>1),"' method='post'> Login <input type='text' name='username' value='test'><br /> Pass <input type='text' name='password' value='test'><br /> <input type='Submit'></form>"; } print end_html;

    Sorry for sending you on a wild goose chase.

    Hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: A rotten cookie
by LameNerd (Hermit) on Aug 05, 2003 at 05:15 UTC
    I hope this is helpful ... it is untested
    #/usr/bin/perl -w use CGI; my $out = new CGI; my $user = $out->cookie( "user" ) || "No Cookie!"; print $out->header( 'text/html' ); print $out->start_html( -title=>"Cookie show" ); print<<HTML; <BODY> $user </BODY> </HTML> HTML