Reputation: 7792
I am writing a webpage in Perl. I write out the Content Type with this line at the beginning of the script:
print header('Content-Type' => 'text/HTML');
This work in Chrome and Firefox, but in IE, it still try to download the page as a file. What should I do to also make IE work?
Upvotes: 0
Views: 1986
Reputation: 24073
You can see the generated headers by running on the command line:
perl -MCGI=:standard -we 'print header( q{Content-Type} => q{text/html} )'
Outputs:
Status: text/html Content-Type: Content-Type; charset=ISO-8859-1
This is obviously not what you want. There are several ways to pass parameters to the header
function. You are calling it like this:
header($content_type, $status);
If you want to call header
with named parameters, you have to prefix them with a dash. Anything other than the recognized parameters -type
, -status
, -expires
, and -cookie
will be stripped of the initial dash and converted to a header, so both of the following work:
perl -MCGI=:standard -we 'print header( q{-Content-Type} => q{text/html} )'
perl -MCGI=:standard -we 'print header( q{-type} => q{text/html} )'
Outputs:
Content-Type: text/html; charset=ISO-8859-1
However, text/html
is the default value for Content-Type
, so you don't need to specify the Content-Type
at all:
perl -MCGI=:standard -we 'print header()'
Outputs:
Content-Type: text/html; charset=ISO-8859-1
Upvotes: 4
Reputation: 123471
I assume you use CGI.pm. CGI::header does not set a single header, but sets the full HTTP header. From the documentation/examples of header:
print header('text/plain');
I think you confused CGI::header with a similar named function in PHP. But same name does not mean same functionality.
Upvotes: 0
Reputation: 241998
The CGI documentation shows just -type
as the key for Content-Type in header
. If it's the only argument, you can omit it and only specify the value.
header( -type => 'text/html' );
# or even
header('text/html');
Upvotes: 3
Reputation: 7792
Fixed by changing the line to:
print "Content-type:text/html\n\n";
Not sure why the original didn't work, but anyways.
Upvotes: 0