in reply to Can't set cookie with CGI.pm

(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.

Replies are listed 'Best First'.
Re^2: Can't set cookie
by Anonymous Monk on Sep 22, 2004 at 05:15 UTC

    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