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

IM trying to create a coookie in Perl but i cant seem to do it.

$cookies is a hash of the info i want to store ($cookie->{'postcode'}....).

The print "set cookie..." should create thr cookie but it doesnt.

Could anyone please help me...please

sub set_cookie <br> { <br> my ($expires,$domain,$path,$secure,$cookie) = @_; <br> my(@days) = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");<br> my(@months) = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Se +p","Oct","Nov","Dec");<br> my($seconds,$min,$hour,$mday,$mon,$year,$wday) = gmtime($expires) +if ($expires > 0); #get date info if expiration set.<br> $seconds = "0" . $seconds if $seconds < 10; # formatting of date v +ariables<br> $min = "0" . $min if $min < 10; <br> $hour = "0" . $hour if $hour < 10; <br> my(@secure) = ("","secure"); # add security to the cookie if defin +ed. I'm not too sure how this works.<Br> if (! defined $expires) <Br> { <br> $expires = " expires\=Fri, 31-Dec-1999 00:00:00 GMT;"; <br> } # if expiration not set, expire at 12/31/1999 elsif ($expires == -1) <br> { <br> $expires = ""; <Br> } <br> else <br> { <br> $year += 1900; <br> $expires = "expires\=$days[$wday], $mday-$months[$mon]-$year $ +hour:$min:$seconds GMT; "; <br> }<br> if (! defined $domain) { $domain = $ENV{'SERVER_NAME'}; } #set d +omain of cookie. <br> if (! defined $path) { $path = "/"; } #set default path = "/"<br +> if (! defined $secure) { $secure = "0"; }<br><br> my $key;<br> foreach $key (keys %$cookie) <br> {<br><br> $cookie->{$key} =~ s/ /+/g; <br><br> print "Set-Cookie: $key\=$cookie->{$key}; expires=$expires; path=$ +path; domain=$domain \n\n"; <br><br> }<br><br> }<br>

Replies are listed 'Best First'.
Re: Cant create a cookie! Please help
by Hot Pastrami (Monk) on Mar 22, 2001 at 00:07 UTC
    Make it easy on yourself... use CGI:
    use CGI; use CGI::Cookie; $cookie1 = new CGI::Cookie(-name=>'cookie',-value=>'some value');
    See this for more details.

    Hot Pastrami
Re: Cant create a cookie! Please help
by fpi (Monk) on Mar 22, 2001 at 00:22 UTC
    When doing cookies for the first time on any project, I usually do it without any extra settings. I suggest first commenting out the domain, path, and expires settings. Just set the name and value of the cookie and see if you can get that to work. And getting it to work can be tricky, because you need the exact syntax in your http header, especially since you are not using the CGI module. Besides, those settings have default values.

    The domain and path are set for the most part as a security or control, to restrict which sites can read the cookie. If you don't specify the domain/path, I believe it will just default to the domain of the site.

    If you don't specify an expire time, I believe your browser will default to the duration that the browser process remains active.

    Even if you try the CGI module, don't use the domain, path, and expires settings right away until you know you are able to set the cookie.
Re: Cant create a cookie! Please help
by dvergin (Monsignor) on Mar 22, 2001 at 00:42 UTC
    What Hot Pastrami said -- for anything that needs to be robust.

    On the other hand 're-inventing the wheel' can be an instructive exercise. Makes you appreciate the standard modules all the more when you struggle to cover all the bases as well as they have done.

    It's quite possible your code is not working for some reason outside this subroutine. Try substituting a plain-vanilla version:

    sub set_cookie { print "Set-Cookie: testkey=testvalue\n"; }
    If that doesn't work, your problem lies in the context from which you are calling the routine. E.g. is your http header completed with a blank line before you call this routine?

    If it does work, then build up to your more general version step-by-step until you figure out what was breaking your code.

    ...Then set that all aside as a learning exercise and use CGI; as Hot Pastrami wisely suggests.

Re: Cant create a cookie! Please help
by Hero Zzyzzx (Curate) on Mar 22, 2001 at 23:42 UTC

    I reinvented the wheel around this once and had a non-working cookie issue also. It was because I was printed the "Content-type:text/html" header before trying to set the cookie.

    For it to work, the set-cookie header must be the very first one to print, it won't work after the content-type header. Maybe this will help you.

    I use CGI to set my cookies now. Beautifully simple.