The Silent Killer: Why Headers Fail After Output
It is one of the most frustrating moments in web development: your form submits successfully, you see the data hit the server, and yet the user remains stuck on the same page. The browser refuses to follow the redirect instruction provided by your PHP script.
In the scenario described, the code attempts to send an email via the mail() function and then issues a header('Location: ...') call followed immediately by an exit(). The logic seems sound at first glance. If $mail is true, it echoes 'OK' and then tries to redirect. The fact that data submission works confirms the HTTP body is being sent correctly; the problem lies strictly in the HTTP response headers.
The root cause is almost certainly output buffering or premature output before the header command executes. In PHP, header() relies on sending raw bytes to the browser before any content appears in the response stream. Once a single characterâwhether it be whitespace, a newline, or even an invisible BOM (Byte Order Mark) from an editorâis sent to the client, the HTTP headers are considered "sent." At that point, calling header() triggers a warning (headers already sent), and the browser ignores the redirect command entirely.
Looking closely at the provided code snippet:
echo 'OK';
header('location: http://www.google.com.au/');
exit();
The line echo 'OK'; is the culprit. It outputs a string to the response stream. Even if this line were removed, other invisible characters might still be present. If config.php includes any whitespace at the top of the file, or if the PHP opening tag <?php has preceding spaces (a common occurrence when copying code from Word or certain IDEs), those bytes are sent before the header logic runs.
Furthermore, relying on mail() directly in production environments is a significant architectural trade-off. While convenient for quick prototypes, it bypasses modern queueing systems and makes debugging difficult because delivery status is binary (success/fail) without granular logs. If the server configuration restricts outbound mail or if the message format triggers spam filters, the script returns false, but no specific error context is provided to the user.
To resolve this, you must ensure absolutely no output precedes the header() call. The most robust pattern involves validating input first, then preparing the response, and finally sending headers only after all logic is complete. However, echoing debugging information like 'OK' before a redirect is an anti-pattern that should be removed entirely in production code.
If you are migrating away from raw PHP scripts or handling complex routing needs, frameworks like Laravel offer a safer abstraction for form submissions. They manage the lifecycle of the request-response cycle and provide robust tools for handling redirects without worrying about header state management. For instance, using Laravel's redirect() helper automatically handles the necessary headers without risking premature output issues.
// Conceptual example in a framework context
if ($mail) {
// Instead of echoing or manually calling header(), use the framework helper
return redirect('http://www.google.com.au/');
}
Additionally, consider moving the header() call to the very top of your script, before any include statements that might inadvertently output data. While the provided code includes config.php, that file often contains database connections or view renderers that might trigger unexpected output. Ensure config.php is strictly configuration-only and does not contain echo statements or print_r calls unless explicitly needed for debugging, and even then, those should be disabled in production environments.
Finally, avoid hardcoding the redirect URL as a string literal if possible. Using relative paths or named routes (in frameworks) reduces the risk of typos and makes the application more maintainable. The solution isn't just about fixing the current error; it is about restructuring how success states are communicated to the client to prevent fragile dependencies on specific output orders.
