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

Hi Monks

I'm having some probs with a script i made, it is setting a cookie but it is printing it out at the top of the browser rather than actually setting a cookie.. I am using Matt Wright's cookie.lib and here is my code:
print "Content-type: text/html\n"; &SetCookies('name',$uname); &SetCookies('pass',"$enc_cert"); print "\n";

It is just printing a 'Set-Cookie: pass=wgfwrgr' line at the top of the browser

Any ideas ?

Thanks

Replies are listed 'Best First'.
(Ovid) Re: Cookie Problem
by Ovid (Cardinal) on Nov 21, 2001 at 23:30 UTC

    I would suggest that your problem is using Matt Wright's cookie library. Try CGI.pm and you'll find that it is all integrated for you. From the CGI documentation:

    $cookie = $query->cookie(-name=>'sessionID', -value=>'xyzzy', -expires=>'+1h', -path=>'/cgi-bin/database', -domain=>'.capricorn.org', -secure=>1); print $query->header(-cookie=>$cookie);

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Cookie Problem
by cfreak (Chaplain) on Nov 22, 2001 at 00:06 UTC
    Hmmm while all the above posters are correct in their comments. The actual problem is that you printed the header before setting the cookie. You need to set the cookie first and then print the header like this:
    &SetCookies('name',$uname); &SetCookies('pass',"$enc_cert"); print "\n"; print "Content-type: text/html\n\n";

    Because everything that comes after the content-type is considered by the browser to be part of the page

Re: Cookie Problem
by VSarkiss (Monsignor) on Nov 22, 2001 at 00:01 UTC

    Ovid has given the best answer above. You may also want to take a look at our own davorg's NMS project here. Unfortunately, NMS doesn't have a replacement for Cookie stuff yet.

    One thing to check: make sure your $uname variable doesn't have a trailing newline. That will appear to terminate the HTTP headers early -- I'm guessing that's what is happening because the first Set-Cookie isn't showing.

    Again, even if this is a correct guess, and your code starts to work, listen to Ovid: use CGI!.

    HTH

Re: Cookie Problem
by atlantageek (Monk) on Nov 21, 2001 at 23:40 UTC
    Actually, SetCookies is plural try the following:
    print "Content-type: text/html\n"; &SetCookies('name',$uname, 'pass', "$enc_cert"); print "\n";

    ----
    I always wanted to be somebody... I guess I should have been more specific.
Re: Cookie Problem
by staeryatz (Monk) on Nov 21, 2001 at 23:44 UTC
    It looks like you only have one '\n' after 'Content-type: text/html'. You should have a whole blank line following the content type.
      thaks guys, none of the above worked though - still printing the cookie line on top of the browser