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

Why does this fail? I have turned on prompt on cookie in my browser so I know the coookie is being set Then when I try to check for the cookie in the logedin sub it fails. I am missing something here, what am I doing wrong??? Thanks :)
my $foo = crypt($password3, $row[0]); if ($foo ne $row[0]) { print "Content-type: text/html\n\n"; print "sorry it seems you are unknown to our database"; } else { set_cookie($username); } } sub set_cookie { my $cookie = $q->cookie ( name => "login", value => $username, path => "/cgi-bin"); print $q->redirect( -url => "http://www.foo.com/cgi-bin/bar/ind +ex.cgi?page=logedin", -cookie => $cookie ); } 1; =================================================== sub logedin { my $cookie = $q->cookie( -name => "login" ); if (defined $cookie) { my $tmpl = new HTML::Template( filename => "../templates/main. +html"); $tmpl->param( username => $cookie ); print "Content-type: text/html\n\n"; print $cookie; #print $tmpl->output; } else { print "Content-type: text/html\n\n"; print "not logged in"; } } 1;

Imagination is more important then knowledge -Einstein-

Replies are listed 'Best First'.
Re: CGI.pm and the missing cookie
by Zaxo (Archbishop) on Oct 25, 2001 at 04:34 UTC

    A couple of things. You need to pass the live CGI query to the subs at runtime, and you want to use the $username value you pass to set_cookie:

    sub set_cookie { my ( $q, $username) = @_; my $cookie = $q->cookie ( name => "login", value => $username, path => "/cgi-bin"); print $q->redirect( -url => "http://www.foo.com/cgi-bin/bar/index. +cgi?page=logedin", -cookie => $cookie ); }
    with corresponding changes in sub logedin{} and the calls.

    After Compline,
    Zaxo

Re: CGI.pm and the missing cookie
by Armos (Scribe) on Oct 25, 2001 at 04:26 UTC
    I'm not certain if this is your problem, but you are not setting an expiration time... if you expect your cookie to persist after you close the browser that originally received it, it won't.

    Again, I'm not sure this is the problem, just a quick observation,
    -A
Re: CGI.pm and the missing cookie
by olly (Scribe) on Oct 25, 2001 at 04:22 UTC
    when I say fails I mean it cannot retieve the cookie
      It is ment to be a browser session cookie only but thanks anyhow :)

      Imagination is more important then knowledge -Einstein-