What does “Premature end of script” mean and how do I fix it?

My web server has been throwing “Internal Error 500″s and “Premature end of script”s. What does this mean and how do I resolve it?


The “Premature end of script headers” error message is probably the most loathed and common error message you’ll find in the Apache world. What the error actually means, is that the script stopped for whatever reason before it returned any output to the web server. You can get this regardless of the type of website you’re running, whether you’re selling apidexin, ballet shoes or just have a personal blog.

A common cause of this for script writers is to fail to set a content type before printing output code. In Perl for example, before printing any HTML it is necessary to tell the Perl script to set the content type to text/html, this is done by sending a header, like so:

print "Content-type: text/html\n\n";

If you don’t do that, Perl will try to output to STDOUT (Standard Output, or the servers terminal) instead of through the web server to your browser.
The same applies to any CGI script of any language.

The problem might be the script itself, for example, say you have a formmail type script that is trying to send an e-mail to sendmail on the server, but the path to sendmail in the script is wrong or has passed bad parameters. The script should die at this stage and print an error message to the server error log, saying that it was “unable to open sendmail” or some such error, but you will see an error like “Internal Server error” or “Premature end of script headers” because printing detailed error messages to an unknown users browser is giving to much potentially damaging information away to possibly untrustworthy people.

With Perl there is something you can try that will attempt to redirect error messages to the browser so you can attempt to diagnose the problem.
Open the script in your text editor, and under the path to Perl on the first line, place this:

use CGI::Carp qw(fatalsToBrowser);

Then upload the script again, set it to executable and point your browser at it. If all goes according to plan, the error message will now be printed to the browser for all to see. Fix the error it details, and then comment out (or remove) that line because its never a good idea to give server settings out.

Leave a Reply

Your email address will not be published. Required fields are marked *