in reply to Header Visible In CGI output after cookie header is sent
The header function behaves as it should. The reason you get the so called eyesore, is because the header has already been printed, and you're attempting to print it again, so at this point, it might be formatted like a header, but it's treated as content. The key value pairs that make up the http header are usually referred to as "http headers" but they're just fields of the http header. The http header is terminated by a "\n\n", which is what most people leave out when first printing a header on their own, and that is the purpose of the header function, to make sure a valid header is printed (one terminated with \n\n)....#### Method: header # Return a Content-Type: style header # #### 'header' => <<'END_OF_FUNC', sub header { my($self,@p) = self_or_default(@_); my(@header); return undef if $self->{'.header_printed'}++ and $HEADERS_ONCE; my($type,$status,$cookie,$target,$expires,$nph,$charset,$attachmen +t,@other) = rearrange([['TYPE','CONTENT_TYPE','CONTENT-TYPE'], 'STATUS',['COOKIE','COOKIES'],'TARGET', 'EXPIRES','NPH','CHARSET', 'ATTACHMENT'],@p); $nph ||= $NPH; if (defined $charset) { $self->charset($charset); } else { $charset = $self->charset; } # rearrange() was designed for the HTML portion, so we # need to fix it up a little. foreach (@other) { next unless my($header,$value) = /([^\s=]+)=\"?(.+?)\"?$/; ($_ = $header) =~ s/^(\w)(.*)/$1 . lc ($2) . ': '.$self->unescapeH +TML($value)/e; } $type ||= 'text/html' unless defined($type); $type .= "; charset=$charset" if $type ne '' and $type =~ m!^text/ +! and $type !~ /\bcharset\b/; # Maybe future compatibility. Maybe not. my $protocol = $ENV{SERVER_PROTOCOL} || 'HTTP/1.0'; push(@header,$protocol . ' ' . ($status || '200 OK')) if $nph; push(@header,"Server: " . &server_software()) if $nph; push(@header,"Status: $status") if $status; push(@header,"Window-Target: $target") if $target; # push all the cookies -- there may be several if ($cookie) { my(@cookie) = ref($cookie) && ref($cookie) eq 'ARRAY' ? @{$cookie} + : $cookie; foreach (@cookie) { my $cs = UNIVERSAL::isa($_,'CGI::Cookie') ? $_->as_string +: $_; push(@header,"Set-Cookie: $cs") if $cs ne ''; } } # if the user indicates an expiration time, then we need # both an Expires and a Date header (so that the browser is # uses OUR clock) push(@header,"Expires: " . expires($expires,'http')) if $expires; push(@header,"Date: " . expires(0,'http')) if $expires || $cookie +|| $nph; push(@header,"Pragma: no-cache") if $self->cache(); push(@header,"Content-Disposition: attachment; filename=\"$attachm +ent\"") if $attachment; push(@header,@other); push(@header,"Content-Type: $type") if $type ne ''; my $header = join($CRLF,@header)."${CRLF}${CRLF}"; if ($MOD_PERL and not $nph) { my $r = Apache->request; $r->send_cgi_header($header); return ''; } return $header; } END_OF_FUNC
I suggest you learn more about HTTP and CGI, as well as read CGI.pm (in this case at least the header funtion), and you'll see why the answer to your question is yes.
|
|---|