Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

I am having problems setting a Cookie

by chuleto1 (Beadle)
on Sep 21, 2002 at 16:45 UTC ( [id://199767]=perlquestion: print w/replies, xml ) Need Help??

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

Good morning monks:
I am having problems setting a cookie Can anyone look at my code and help me out?
#one.pl #!/usr/bin/perl use strict; use CGI; my $cgi = new CGI; print $cgi->header; print $cgi->start_html('A Simple Example'), $cgi->h1('A Simple Example'), $cgi->start_form(-action=>"test.pl"), "What's your name? ",$cgi->textfield('name'), $cgi->p, $cgi->submit, $cgi->end_form, $cgi->end_html;


#test.pl #!/usr/bin/perl use strict; use CGI::Cookie; use CGI; my $cgi = new CGI; print $cgi->header( -cookie => new CGI::Cookie(-name => 'name', -value=> $cgi->param( +'name'), -expires => 'Thu, 26- +Sep-2002 00:00:00 GMT', -domain => 'students. +cs.byu.edu' ) ). $cgi->start_html(-title => 'A Simple Example', -bgcolor => $cgi->param('color')); if ($cgi->param()) { print $cgi->center( $cgi->h3("Hello: ". $cgi->param('name'))); } print $cgi->start_form(-action => "results.pl"), "What's your name? ",$cgi->textfield('name'), $cgi->p, "What's the combination?", $cgi->p, $cgi->checkbox_group(-name=>'words', -values=>['eenie','meenie','minie','moe'], -defaults=>['eenie','minie']), $cgi->p, "What's your favorite color? ", $cgi->popup_menu(-name=>'color', -values=>['red','green','blue','chartreuse']), $cgi->p, $cgi->submit('SUBMIT'), $cgi->end_form, $cgi->end_html(); exit(0);

#!/usr/bin/perl # results.pl use strict; use CGI::Cookie; use CGI; my $cgi = new CGI; print $cgi->start_html(-title => 'Results', -bgcolor => $cgi->param('color')); if ($cgi->param()) { print $cgi->center( $cgi->h3("Hello: ". $cgi->param('name')), $cgi->p, "The keywords are: ",join(", ",$cgi->param('words')), $cgi->p, "Your favorite color is: ",$cgi->param('color')); } print $cgi->end_html(); exit(0);

-thanks

Replies are listed 'Best First'.
Re: I am having problems setting a Cookie
by dws (Chancellor) on Sep 21, 2002 at 17:09 UTC
    Try adding a path, like this:
    new CGI::Cookie(-name => 'name', -value=> $cgi->param('name'), -expires => 'Thu, 26-Sep-2002 00:00:00 GMT', -domain => 'students.cs.byu.edu', -path => '/' )
Re: I am having problems setting a Cookie
by spartacus9 (Beadle) on Sep 21, 2002 at 23:21 UTC
    While your code looks like it would work (I tested it on my end, altering just the cookie domain), I generally debug my cookie problems by removing everything that is not essential to setting the cookie and one-by-one adding them back in. For example, try setting the cookie without declaring the expiration date, the domain or the path. Just set the name and value. Then test it with Mozilla and afterward use the Cookie Manager in Mozilla to see exactly what was set. Provided that works, then add in the expiration date, then the path, and finally the domain. The most common problem I see with cookie files occurs when messing with the domain attribute. One small mistake and the cookie will never be set because it won't allow you to set a cookie for another domain.

    Hope that helps.
Re: I am having problems setting a Cookie
by zengargoyle (Deacon) on Sep 22, 2002 at 00:07 UTC

    What works for me I copied almost vebatum from CGI Programming with Perl.

    #!/usr/bin/perl -T # # the main page. /cgi-bin/main.pl # use strict; use warnings; use CGI; my $q = new CGI; my $cookie = $q->cookie( -name => 'session_id' ) || set_and_test_cookie( $q ); # if you get here, you have a cookie # use cookie for whatever... exit; sub set_and_test_cookie { my $q = shift; my $server = $q->server_name; my $session_id = $ENV{UNIQUE_ID}; # change to taste my $cookie = $q->cookie( -name => 'session_id', -value => $session_id, -path => '/cgi-bin', -secure => 1, ); print $q->redirect( -url => "https://$server/cgi-bin/cookie_test.pl", -cookie => $cookie, ); exit; } # # end main.pl # #!/usr/bin/perl -T # # check the cookie. /cgi-bin/cookie_test.pl # use strict; use warnings; use CGI; use constant SOURCE_CGI => "/cgi-bin/main.pl"; my $q = new CGI; my $cookie = $q->cookie( -name => 'session_id' ); if ( defined $cookie ) { print $q->redirect( -url => SOURCE_CGI ); # ok, go back. } else { print $q->header( -type => 'text/html' ), $q->start_html( "Cookies Disabled" ), $q->h1( "Cookies Disabled" ), $q->p( "Your browser is not accepting cookies. You need them. Ena +ble them and ", $q->a( { -href => SOURCE_CGI }, "Try Again" ), '.' ), $q->end_html; } # end cookie_test.pl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://199767]
Approved by Aristotle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found