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

Hy friends. I have a problem with "Set-Cookie".
How to post the "Set-Cookie" in my script?
My script:
#!/usr/bin/perl require "admin_sub.pl"; require "uzual_sub.pl"; require "variabile.pl"; &readForm; if ($FORM{'verify'} eq "account") { if (($FORM{'user'} eq "$adminUser") && ($FORM{'pws'} eq "$adminPas +sword")) { print "Content-type: text/html\n\n"; print "Set-Cookie: .$user.=.$pws.; expires=..; path=./.; domain=.l +ocalhost.;"; print "The cookie is: $ENV{'HTTP-COOKIE'}"; exit; } else { print "Content-type: text/html\n\n"; print "Username-ul sau parola nu sunt bune."; exit; } exit; } print "Content-type: text/html\n\n"; print <<ENDHTML; <html> <head> <title>$websiteTitle :: Shop Admin.</title> </head> <body> <table align=center border=0> <form action="http://localhost/cgi-bin/larrycenter/admin.pl" method="p +ost"> <input type=hidden name="verify" value="account"> <tr><td> User: <input type=text name="user"><br> Password: <input type=password name="pws"><br> <input type=submit value="Log in!"><br> </td></tr> </form> </table> </body> </html> ENDHTML exit;
Please help me.

edited: Fri Jul 11 01:12:19 2003 by jeffa - code tags, formatting

Replies are listed 'Best First'.
Re: Help to eat cookie
by hardburn (Abbot) on Jul 10, 2003 at 17:29 UTC

    You want to use the CGI module (which comes by default with any reasonably up-to-date version of Perl). Setting a cookie with it is as simple as:

    use CGI; my $cgi = CGI->new(); $cookie = $cgi->cookie( -name => 'cookie_name', -value => 'cookie_value', ); # CGI->header also takes care of outputing the Content-type print $cgi->header( -cookie => $cookie );

    That's just a basic overview. You'll want to read the complete documentation for the module (linked to above) in order to integrate CGI.pm more fully into your CGI. Most importantly, you'll want to convert your CGI to use CGI.pm's param fetching routines.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Help to eat cookie
by cfreak (Chaplain) on Jul 10, 2003 at 17:59 UTC

    Cookies must be set before content header so if you take out your first print "Content-Type: text/html\n\n"; and your code should work (barring any other errors of course)

    However to echo what hardburn was saying the CGI module has functions that not only will handle your cookies and your headers, but can also help you to create forms and retrieve them.

    You seem new here to Perlmonks so I'll give you the standard stuff: you should always use strict; (See Why you should use strict) and while testing its also a good idea to use warnings;, also for web programming see use CGI or die;, to learn lots of things about CGI and why its a good idea.

    Lobster Aliens Are attacking the world!
      But whay dont show the cookie? The $ENV{'HTTP-COOKIE'} dont show the cookie. Whay?
Re: Help to eat cookie
by cleverett (Friar) on Jul 11, 2003 at 00:22 UTC
    The comments about using CGI.pm are certainly valid, and I'd certainly go along with them in the context of CGI scripts.

    But just to answer the specific question, your problem is that you're printing the two newlines signifying the end of the HTTP headers, and then doing the Set-Cookie. Try this instead:

    print "Set-Cookie: .$user.=.$pws.; expires=..; path=./.; domain=.local +host.;"; print "Content-type: text/html\n\n";

    Be Well.