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

Hi all, I'm probably doing something super obviously wrong, but I can't see it. The script:
#!/usr/local/bin/perl use CGI; use CGI::Carp qw(fatalsToBrowser); use strict; my $query = new CGI; my $cookie = $query->cookie( -name => 'test', -value => 'banana' ); print $query->header(-type => "text/html", -cookie => $cookie ) || die "Cannot set cookie $!"; print "Hello";
If I run from the Unix command line:
Set-Cookie: test=banana; path=/ Date: Wed, 22 Sep 2004 03:43:55 GMT Content-Type: text/html; charset=ISO-8859-1 Hello
However, when running via a webserver, the script dies when it tries setting the cookie. (If I remove the "die" statement, it prints "hello". What have I missed? Cheers

Replies are listed 'Best First'.
Re: Can't set cookie
by ikegami (Patriarch) on Sep 22, 2004 at 04:33 UTC

    (As an aside, the CGI module doesn't set $!, so it's useless to print its value. And more specifically, CGI::header() doesn't ever return an error.)

    Are you running under mod_perl? CGI::header() always returns '' (which is false) when running under mod_perl. The headers are sent by calling $r->send_cgi_header($header);, where $header is the string normally returned to be printed. The statement will always die in mod_perl.

      Hmm, it is a CGI script running under mod_perl via Apache::Registry (which I did not know at the time). It runs fine from a standrd CGI directory. Because I'm not using a mod_perl handler in the code, I guess I can't call "$r->send_cgi_header($header);" Is this correct?

      My understanding - and it may be incorrect - is that the CGI.pm $query->header should send the header (according to this doc anyway) I've tried setting PerlSendHeader On and Off, but to no avail

        It does send the header, before header() even returns. Your code should be:

        #!/usr/local/bin/perl use CGI; use CGI::Carp qw(fatalsToBrowser); use strict; my $query = new CGI; my $cookie = $query->cookie( -name => 'test', -value => 'banana' ); print $query->header(-type => "text/html", -cookie => $cookie ); print "Hello";

        under mod_perl (and if not an NPH script), that's the same as:

        #!/usr/local/bin/perl use CGI; use CGI::Carp qw(fatalsToBrowser); use strict; my $query = new CGI; my $cookie = $query->cookie( -name => 'test', -value => 'banana' ); $query->header(-type => "text/html", -cookie => $cookie ); print "Hello";

        which DOES send the header. Look at the source of CGI.pm

Re: Can't set cookie
by Errto (Vicar) on Sep 22, 2004 at 03:52 UTC
    the script dies when it tries setting the cookie.

    Please elaborate. In what way does it die? I tried it here and it works fine.

    Update: ikegami's suggestion below is correct if you are under mod_perl. I had a different problem when I tried to run the script from my personal home directory which turned out to be an Apache suexec error due to file permissions. chmod g-w filename.pl fixed it in that particular situation.

    Update 2: sorry I didn't read the part where it was working without the die. See below.