September 30, 2005

Premature end of script headers

As anyone dealing with Perl .cgi scripts may have run into the dread premature end of script headers error, I thought I'd post the above link from htmlfixit.com. The title is apt: "Server error messages, the bane of web developers everywhere".

That page, intelligibly written for those of us who are only somewhat versed in Perl coding, helped me to debug a script recently. Actually, the answer wasn't on the page, but it did give me the hint/clue that solved my problem. :)

<Added>

And this is terrific for debugging errors (put it right under the path to Perl). What it does it send the error message to the browser.

use CGI::Carp qw(fatalsToBrowser);

~ Diane Vigil

12 Comments to "Premature end of script headers"

  1. Michael Bianchini says:

    Hello! I tried using the use command listed above. I put it right underneath the first line of my script so that it looks like this:

    #!/usr/bin/perl
    use CGI::Carp qw(fatalsToBrowser);

    etc. And when I run I just get the default message:

    Server error!

    The server encountered an internal error and was unable to complete your request.

    Error message:
    Premature end of script headers: FormMail.cgi

    If you think this is a server error, please contact the webmaster

    Error 500

    localhost
    Sun 23 Jul 2006 10:39:35 AM EDT
    Apache/2.0.40 (Red Hat Linux)

    I am running Red Hat Linux v9, Mozilla Firefox v1.0, and Perl v5.8.0 and the Apache Web Server v2.0.40.

    Any suggestions?

    Thanks!

  2. Diane Vigil says:

    Hi. Did the script run at all before you inserted that line? And were the permissions set correctly?

    There's more information here: Server error messages, the bane of web developers everywhere.

  3. Michael Bianchini says:

    Hello! Thanks for responding!

    I was able to solve my original problem (same error) without actually using the fatalstoBrowser. You're right, I had to open up execute permissions for all users on my home directory, which eventually led to the directory that I was using as my DocumentRoot.

    So yes, the script now runs fine without that line. If I put the line back in, I still get the error. I had already looked at the link you mentioned, but it simply tells me to use the fatalstoBrowser. I found another link (URL provided) that seems to indicate that "some servers do not support this technique." Could it be that mine doesn't? Old version of Apache or something?

    Thanks again for your help. At least my script is working now! :)

    - Mike

  4. DianeV says:

    Yes, that would be it — not all servers are set up to run everything.

    I'm glad the script runs now. And, yes, you have to set the permissions on Perl scripts so that they can be executed via the website (755). It's always something to remember. :)

  5. murali says:

    Thank you! It works. It returned the actual error (which was that I had put a " in a " bracket, a big no no!

    Like this:
    $errmsg = ""Username" mesti di isi!\n" ;

    It should be:
    $errmsg = "'Username' mesti di isi!\n" ;

    Thanks!

  6. Diane Vigil says:

    Excellent, Murali! Glad it was of help — and thanks for your explanation. No doubt it will help others.

    However, I think your comment was meant to be here:
    WordPress & Akismet: a little code blocker

  7. Franki says:

    Hi guys,

    That line:
    use CGI::Carp qw(fatalsToBrowser);

    will not work if the script can't run. What I mean is that the above line is a line of code. If the script has been uploaded as binary, or the permissions are wrong, it will not get run and therefore will not send anything useful to the browser.

    It's saved me lots of time troubleshooting other problems though.

    regards

    Franki
    htmlfixit.com

  8. Diane Vigil says:

    Good point, Franki. That's one of those things that goes unmentioned because it's so obvious; the kind of thing that I just hate when I run into it. And now I've done it, too. :) Thanks for pointing it out.

  9. John Miller Crawford says:

    Brilliant! Thanks so much, Diane, for showing up in Google on a "Premature end of script headers" search - immediately told me what was going wrong (Can't localize lexical variable).

  10. Diane Vigil says:

    You're very welcome, John. Glad it was of help to you.

  11. Sneha says:

    Thank you so much, I google whole day but did not find anything. But this post seems useful. It helped me to figure out the error. I was banging my head in wall whole for "Premature end of script headers". Thank you!! You saved me from trouble!

  12. Diane Vigil says:

    You're very welcome, Sneha — and I truly understand what you mean!

Have your say ...

If this is your first comment, it will be held for moderation (but comments are appreciated). Otherwise, just be courteous, don't drop links unless highly pertinent — and no substituting keywords for your name. Posters must be 16 or older. We use Akismet, so if you don't see your non-spam comment posted, contact me.

Manage your subscriptions

Archives
© 2004-2008 DianeV. Web Design Studio - Los Angeles. All Rights Reserved.
27 queries. 0.168 seconds.