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

I have a very simple piece of code, stripped down to it's barest that should simply set a cookie on the users system (I'm using CGI.pm) and it doesn't work. Help me fellow monks!

#!/usr/bin/perl

use CGI;
use CGI::Carp (fatalsToBrowser);

$co = new CGI;

$cookie = $co->cookie(
	-name=>'test',
	-expires=>'+1h',
	-value=>'saveme'
);

print $co->header(-cookie=>$cookie);
print 'output';

No apart from the fact that I haven't bothered sending a proper HTML page (stripped to the barest remember) why won't this snippet make a cookie?

Replies are listed 'Best First'.
RE: Cookie Failure
by steveAZ98 (Monk) on Aug 02, 2000 at 21:58 UTC
    I've had various problems with cookies working if I didn't set the path for the cookie. I currently use this code and it works for me. You should also think about setting the domain.
    my $cook = $q->cookie( -name => 'uid', -value => $s->info->uid, -path => '/' ); $s->set ? print $q->header(-'cookie'=>$cook, -'type'=>'text/html') + : print $q->header(-'type'=>'text/html');
    HTH
RE: Cookie Failure
by agoth (Chaplain) on Aug 02, 2000 at 21:29 UTC

    Which bit exactly isnt working? the output is wrong or the cookie doesnt get set

    >perldoc CGI::Cookie

    has different syntax to yours, give that a spin see if it makes any difference?

      I tried CGI::Cookie with no effect on the results, still the page is displayed and no cookie is stored. The code now looks as follows:

      #!/usr/bin/perl use CGI; use CGI::Carp (fatalsToBrowser); use CGI::Cookie $co = new CGI; $cook2 = new CGI::Cookie(-name=>'test2', -expires=>'+1h', -value=>'sav +eme2'); $cook = $co->cookie( -name=>'test', -path=>'/', -expires=>'+1h', -value=>'saveme' ); print $co->header(-cookie=>[$cook,$cook2]); print 'output';

      Nothing changes either if I try to only use one cookie, nomatter which one of the two it is. ...-path=>'/'... has no effect on either - it defaults to '/' as I said before.

Re: Cookie Failure
by slurp (Initiate) on Aug 03, 2000 at 02:46 UTC
    I think you need to specify the content-type too,ex:
    print $co->header('-type'=>'text/html','-cookie'=>$cookie);
    -- slurp
      Once again, the docs say it defaults to text/html, which it does, and adding ...-type=>'text/html'... or ...'-type'=>'text/html'... (which I did try, just in case) makes no difference, the printed output still appears, but no cookie! This is driving me insane!

        Your code on my machine is correctly setting both test and test2 cookies, viewed with Mozilla. How are you determining whether the cookie is there or not?

        Im not being patronising by hoping youre not expecting to see your cookies in the browser window are you?

        Therefore if youre code is fine, check the cookie validation youre using, browser settings etc?

         

Re: Cookie Failure
by Merlin83 (Novice) on Aug 02, 2000 at 23:15 UTC
    Well output (from print 'output';)gets printed, as would any other stuff I put down there, like a properly formatted HTML document or something.

    I haven't tried CGI::Cookie, but I will soon. It shouldn't really be necessary, since CGI.pm itself has cookie functionality with the cookie() method, as stated all over the docs and tutorials and stuff and in perldoc CGI. I'd prefer to stick with just CGI.pm but if no-one can come up with a solution for that I guess CGI::Cookie will have to be the answer (assuming that works).

    As for the path, according to the docs it defaults to '/' but even setting it to that explicitly doesn't make any difference. SteveAZ98, you seem to have ...->header(-'cookie'=>$cook... which is not what the docs say, unfortunately it doesn't work for me though. Any more ideas?