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


salutations monks,

i am working with cookies, and i am getting thrown off by placing a cookie. the documentation says:
print $query->header(-cookie => $cookie);
though i am confused, because if you are working with a webpage, then you have to print out "Content-type:text/html\n\n" right? so if you do:
print "Content-type:text/html\n\n"; $query = CGI->new(); # cookie stuff print $query->header(-cookie => $cookie);
it just prints the cookie to the browser... but if i switch, or get rid of the print "Content-type:text/html\n\n"; then it works fine... and i missing something simple here?

thanks for bearing with me monks

Replies are listed 'Best First'.
Re: CGI cookies
by Ionizor (Pilgrim) on Jan 15, 2003 at 17:58 UTC

    Printing \n\n at the end of one of your headers tells the browser that you've finished sending headers so the line printed to the browser by your $query->header(); call will be interpreted as content.

    As long as you're using the CGI header method anyway, why not just let it handle the content type with -type => 'text/html', e.g.:

    print $query->header(-cookie => $cookie, -type => 'text/html');

Re: CGI cookies
by Bilbo (Pilgrim) on Jan 16, 2003 at 10:08 UTC

    The header method in the CGI module returns the content-type header. If no argument is supplied then the content type defaults to text/html, so

    print $query->header()

    would do exactly the same as your

    print "Content-type:text/html\n\n";

    If you include other arguments (such as -cookie => $cookie) then these are included in the return header as well.