HTTP header issues with Perl CGI module

HTTP header issues with Perl CGI module

I am troubleshooting a Perl script which uses the CGI module. My difficulty is that I need to parse the parameters that the browser passes to the CGI script, and generate one of several possible HTTP header types based on the parameters provided. (Specifically, the request is generated when the user activates a scroll button on the web page. If the user scrolls past the end of the array on the server-side database, I want the CGI script to respond by generating an HTTP redirect header.)

My problem: the CGI module appears to require that I explicitly generate a header immediately after the “use CGI” statement, and before I call any other CGI module functions. If I do not do this, then the Apache server generates a default HTTP header that will precede the header that I ultimately generate from my program. And the default header will have a type of “text/plain.” The result is that my HTML page is displayed as text by the browser, including the HTTP header that I intended to be the HTTP header.

I tried to get around this by writing Perl code to parse the CGI script parameters. Unfortunately, the server apparently does not pass the CGI parameters to the Perl script through the command-line; the parameters are accessible only through functions in the CGI module. And this takes me right back to my original problem: the CGI module requires that I generate a header before using these functions.

Any suggestions?


Post some code that demonstrates the problem. It’s probably more complicated than you think.

Try this


use CGI qw(:standard);

$name = param(‘name’) || ‘(none provided)’;

p('Name: ’ . $name),

exit;[/code]Save it as, and call it like

Works for me. Perhaps you also need to use CGI::Carp qw(fatalsToBrowser), you’re probably generating an error somewhere.

:cool: Perl / MySQL / HTML+CSS

I am not seeing any errors returned by the script.

Here are the HTTP headers that are returned. You can see the header with the problematic text/plain MIME type, followed by the header that I generated. Any idea what the “5e1” is about? If this is some spurious junk that one of my scripts is managing to produce, would the Apache server automatically stick an HTTP header in front of it before returning it to the browser? If so, then that is probably the source of my problem.


HTTP/1.1 200 OK
Date: Tue, 08 Feb 2005 07:49:43 GMT
Server: Apache/1.3.31 (Unix) DAV/1.0.3 mod_gzip/ PHP/4.3.10 mod_ssl/2.8.19 OpenSSL/0.9.6c
Transfer-Encoding: chunked
Content-Type: text/plain

Expires: Tue, 08 Feb 2005 07:49:50 GMT
Date: Tue, 08 Feb 2005 07:49:50 GMT
Newstyle-urls: 0
Content-Type: text/html; charset=ISO-8859-1

I finally found the cause of my difficulties. My script called an external subroutine which returns the dimensions of a JPG file. Without delving into the guts of the JPG module, it appears that something happens there which causes the web server to return an HTTP header with a “text/plain” MIME type. So I need to generate my “text/HTML” HTTP header before using this subroutine.

Problem solved.

Thanks for your help!