Error 500: Premature end of script headers

Error 500: Premature end of script headers

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Error 500: Premature end of script headers

The error message "Premature end of script headers: contactform.cgi" is a classic symptom of a mismatch between how your PHP script is being executed and what headers it has already sent to the server. When you moved this script from a standard Apache mod_php environment to a CGI-enabled server, the execution model changed fundamentally. In a traditional setup, Apache handles the HTTP headers automatically based on the file extension (.php). However, in a CGI environment, the web server expects the script itself to be responsible for generating every byte of the response, starting with the status line and content headers.

The root cause lies in this specific block of code at the very top of your file:

#!/usr/local/bin/php

<?php
print "Content-type: text/html\n\n";
use CGI::Carp qw(fatalsToBrowser);
ini_set('display_errors',1);
error_reporting(E_ALL);

In a standard PHP installation, the shebang line #!/usr/local/bin/php tells the operating system to execute the script via the PHP CLI interpreter directly. When the CLI runs, it does not automatically output an HTTP status line (like 200 OK) or set the necessary headers because it is not running inside a web server context; it is a command-line tool. By explicitly using print "Content-type: text/html\n\n"; immediately after the opening <?php tag, you are sending raw data before the CGI environment has a chance to inject its own status header.

This creates a conflict. The web server (running in CGI mode) expects the first line of output to be an HTTP status code. Instead, it receives text content. This violation of the HTTP protocol triggers the "Premature end of script headers" error. The fact that this works on your previous server is likely because that server was using mod_php, where Apache handles all header generation automatically, rendering the manual print statement unnecessary and often redundant.

To resolve this, you must align the execution method with the environment. If the CGI server requires a script named contactform.cgi, it is almost certainly expecting to be executed via the CLI interpreter or a specific CGI wrapper that doesn't support automatic header injection for this specific setup.

Removing Manual Header Output

The most direct fix is to remove the manual header printing and the shebang line, assuming the PHP binary invoked by the server handles the HTTP context correctly. However, if you are invoking this script directly via the CLI path defined in the shebang (e.g., #!/usr/local/bin/php), that approach will fail on a CGI stack unless the wrapper is specifically configured to handle shebangs, which is rare for standard CGI deployments.

Instead, ensure the file is invoked as a standard CGI script without the shebang line if the server's entry point handles it, or rely on the web server's SAPI handler. If you must keep the file executable as a standalone binary, you need to verify that the php-cgi binary (not the CLI version) is being called.

Handling Errors in CGI Environments

You mentioned not having access to the server logs, which makes debugging difficult. In a CGI context, enabling error display via ini_set('display_errors', 1) often causes exactly this kind of crash if an HTTP header has already been sent or if the error occurs before the buffer is flushed correctly.

A better approach for CGI scripts is to redirect errors to the log file rather than printing them directly to the output stream, unless you are certain no headers have been sent yet. You can achieve this by modifying the configuration line:

ini_set('display_errors', 0); // Prevent raw errors from breaking headers
ini_set('log_errors', 1);     // Send details to error_log instead
error_reporting(E_ALL);

This ensures that if a fatal error occurs during validation or mail sending, the server logs the stack trace in error_log rather than trying to print it to the browser, which would corrupt the response stream.

The Mail Function Pitfall

Another subtle issue in your code involves the mail() function call. In CGI environments, some web servers strip the output buffer after a certain amount of time or upon specific signals. If your script takes too long to process validation or send the email, the server might terminate the request before headers are finalized.

Additionally, ensure that the clean_string function does not inadvertently introduce whitespace at the start of $email_message. While less likely with your regex usage, any output sent before the mail() function completes could interfere with the response if the server expects a specific structure.

Conclusion

The script failed because it attempted to define HTTP headers manually while running in an environment that expected the web server to manage them, or because the shebang line forced CLI execution which lacks HTTP context. By removing the manual print statement for headers and configuring error logging to avoid breaking the response stream, you can get the form working on the CGI server. If the issue persists, verify that the PHP binary path in the shebang points to php-cgi rather than the standard CLI interpreter.

Our network