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

Morning monks!

I'm writing a script that will keep a login session using cookies (I know, I should use merlyns browser session technique). The problem I am having is that when ever the script gets the cookie values, they are allways '$user' and '$pass'). I've tried varous methods of setting the cookie values, but they all seem to have the same results.

Here is my code:
#!/usr/bin/perl use CGI; use strict; my $wm = CGI::new(); print $wm->header(); my ($usr,$pw); if ($ENV{'HTTP_COOKIE'}) { $usr = $wm->cookie('user'); $pw = $wm->cookie('pass'); } else { $usr = $wm->param('usr'); $pw = $wm->param('pw'); &set_cookie($usr,$pw,$wm); } print<<LOSER; <html> <body> LOSER if (length($usr) > 0 && length($pw) > 0) { #check login. if it fails, display login form (removed because of +length) #note: right here it would print "Username: $user Password: $pass" #which it should print the cookie values for 'user' and 'pass'. print "Username: $usr Password: $pw<br>\n"; } else { #login form (removed because of length) } print<<END; </body> </html> END sub set_cookie { my $user = shift; my $pass = shift; if (defined($user) && defined($pass)) { # we set cookie # method 1 print "$user asfsfafaf $pass<br>\n"; print "Set-Cookie: user=$user;expires=+1hr\n"; print "Set-Cookie: pass=$pass;expires=+1hr\n"; } }

Any feedback is appreciated.
^jasper

Replies are listed 'Best First'.
Re: Receiving Cookie Values
by dingus (Friar) on Dec 04, 2002 at 15:48 UTC
    Weeellllll, you have to set the cookie in the HTTP header not in the body of the HTML document. So that means you have to do the print header() after the cookie setting stuff. Try
    my ($usr,$pw); if ($ENV{'HTTP_COOKIE'}) { $usr = $wm->cookie('user'); $pw = $wm->cookie('pass'); } else { $usr = $wm->param('usr'); $pw = $wm->param('pw'); } print $wm->header(-cookie=>[$usr, $pw]);
    Also take a look at the CGI.pm docs and examples - especially the cookie setting one

    Dingus


    Enter any 47-digit prime number to continue.
      That didnt work either, here is what the browser says when It prompts to accept the cookie:

      ---
      This page wishes to set the cookie
      wintermarket_members_user="%24user"
      ---
      And its the same thing with the password =\

      I noticed in the 'cookie setting example' that you linked to, it escaped out the hash like so:
      $the_cookie = cookie(-name=>'animals', -value=>\%zoo, -expires=>'+1h');
      Could this have anything to do with it?

      Here is my code:
      my ($usr,$pw); if ($ENV{'HTTP_COOKIE'}) { # from cookie $usr = $wm->cookie('wintermarket_members_user'); $pw = $wm->cookie('wintermarket_members_pass'); } else { # from form $usr = $wm->param('usr'); $pw = $wm->param('pw'); } &set_cookie($usr,$pw,$wm); #... in set_cookie sub my $cookie_usr = $input->cookie(-name=>'wintermarket_members_user', -value=>$usr, -expires=>'+1h'); my $cookie_pw = $input->cookie(-name=>'wintermarket_members_pass', -value=>$pw, -expires=>'+1h'); print $input->header(-cookie=>[$cookie_usr,$cookie_pw]);
      But thanks for the help dingus :)
      ^jasper
        This page wishes to set the cookie
        wintermarket_members_user="%24user"

        OK - %24user is the escaped version of $user. So I think somewhere you are setting the parameter $usr to '$user' not to "$user"

        I think your cookie setting code looks OK. Its that you are settign the wrong values somehow. have you checked the values of param('usr') and param('pw') ?

        Dingus


        Enter any 47-digit prime number to continue.