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

i'm just messing around and trying to see the difference... i can set cookies fine w/ CGI.pm, but if i manually try to set them by printing them out, it doesn't work? this is what i have:
$cookie_id = &random_id; $cookie = "Set-Cookie: cid=$cookie_id; expires=01-Jan-2020 01:01:01 GM +T;\n"; print "Content-type: text/html\n"; print $cookie; print "\n";
what it actually prints out is this:
Content-type: text/html Set-Cookie: cid=MbGFp1L0uIGT50BAlb5wovvwT86thIm5; expires=01-Jan-2020 +01:01:01 GMT;
but there's no cookie set, any ideas why? thanks

Replies are listed 'Best First'.
Re: how to set a cookie without using CGI.pm?
by talexb (Chancellor) on Feb 04, 2005 at 15:20 UTC
      how to set a cookie without using CGI.pm?

    If you do it without using CGI, does this still count as a Perl question? ;)

    OK, enough fooling around. Use the brute force method. Use CGI to set a cookie, and look at the output, then try to duplicate that using just print statements. You'll probably discover that you need a second newline after the Content-type: to get it to work.

    Tinker around a little before posting .. show a little creativity.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: how to set a cookie without using CGI.pm?
by davis (Vicar) on Feb 04, 2005 at 15:22 UTC
    You should try seeing what CGI does:
    perl -MCGI -e 'my $q = CGI->new(); print $q->header(-type=>"text/html +", cookie => $q->cookie(-name=>"foo",-value=>"bar"))'
    This prints the "Set-Cookie" line first, and follows the "Content-Type" with the required double-newline

    davis
    It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
Re: how to set a cookie without using CGI.pm?
by FitTrend (Pilgrim) on Feb 04, 2005 at 18:05 UTC

    Here is an alternative method I've used when playing with cookies and tracking a user's login session. I remember using this because for some reason I couldn't make it work based on some internet examples.

    Here is the subs I used to read and write cookie information:

    sub cookieRead { local(@rawCookies) = split (/; /,$ENV{'HTTP_COOKIE'}); foreach (@rawCookies) { ($key, $val) = split (/=/, $_); $cookie{$key} = $val; } } sub cookieWrite { local($name, $value, $expiration, $path, $domain, $secure) = @_; print "Set-Cookie: "; print ($name, "=", $value, "; path=", $path, "; domain=", $domain, + "; ", $secure, "\n"); }

    Here is the code I use to write cookie information:

    &cookieWrite("session", "$userName::$Password", "$expDate", "/cgi-bin/ +", "fittrend.com");

    Here is the code I use to read cookie informaiton

    &cookieRead; ($cookie{'user'}, $cookie{'pass'}) = split (/::/, $cookie{'session'}); &loadUserSession($cookie{'user'});

    A fews flaws here that I personally worked around. If the user name or password contains a :: then this code could potentially break. Secondly, I recommend that you encrypt this information for security reasons. Using Crypt or another modules on cpan. Lastly, if you have a large amount of data, it won't fit on a single variable in the cookie. I just needed mine to track user name and password so I can use other code to load the entire user's profile from a MySQL back-end.

    At the time, I was having problems with multiple variables in a cookie, so I simply delimited it. If I spent more time on it, I'd probably fix it.

    Hope this helps
    Marc

Re: how to set a cookie without using CGI.pm?
by dragonchild (Archbishop) on Feb 04, 2005 at 16:44 UTC
    Alternately, you can put it into the HTML, within the <head> tag.
    <META HTTP-EQUIV='Set-Cookie' CONTENT='KEY=VALUE; domain=.me.com; path +=/path'>

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: how to set a cookie without using CGI.pm?
by sh1tn (Priest) on Feb 04, 2005 at 15:32 UTC
    Cookie-Set must precede Content-type:
    $cookie = "Set-Cookie: cid=$cookie_id; expires=01-Jan-2020 01:01:01 GM +T;"; print "$cookie"; print "Content-type: text/html\n\n";
        thanks for the replies guys, i think it was because i didn't set the cookie with a path and/or i didn't have the day in date format for the time, works now!