PHP Header Location Redirect Is Not Working
You are encountering a classic PHP error: Warning: Cannot modify header information - headers already sent. This occurs because the HTTP protocol requires all headers to be transmitted before any body content is sent to the client. Once your script outputs even a single characterâwhether it is whitespace, a newline, or an HTML tagâthe headers are considered "sent," and subsequent attempts to use header('Location: ...') will fail.
In your specific scenario, the error message points directly to the culprit: /Applications/MAMP/htdocs/testygubbins/OO/test/header.php:15. Looking at your header.php file, the issue is immediately visible on line 15. The file begins with <?php, which is correct, but it transitions to <html> and then <head> almost immediately.
The problem often stems from invisible characters preceding the opening PHP tag or within the HTML structure itself. If there is a single space, a tab, or a newline character between the closing ?> of the PHP block and the opening <html> tag, that whitespace counts as output. Similarly, if you accidentally left a stray blank line at the very top of the file before <?php, PHP will send that whitespace immediately upon execution, preventing the redirect header from being set later in form.php.
To resolve this, you must ensure zero output occurs before the redirect logic runs. The most robust solution is to strip the closing ?> tag entirely from files that contain only PHP logic or mix logic with HTML without strict separation. By removing ?>, you eliminate the risk of whitespace appearing after the last instruction. If you choose to keep the closing tag, every line following it in header.php must be strictly necessary HTML content, and the file must not contain any trailing newlines before the tag itself.
Consider restructuring your flow to separate concerns more effectively. Instead of embedding logic that triggers a redirect inside a file designed to render layout elements, move the redirection logic into a dedicated controller or action file. For example, if header.php is meant to output the <html>, <head>, and opening <body> tags, it should do so without including conditional logic that might inadvertently trigger an exit before the header is sent.
If you must perform the redirect inside form.php, ensure that no includes or requires happen after the decision to redirect but before the header() call. In your code snippet:
include 'header.php';
// ... some code
header('Location:index.php');
exit;
The include 'header.php'; statement executes first. If header.php outputs any HTML, the headers are sent. You cannot then issue a Location header. The redirect must happen before header.php is included if you intend to bypass the layout entirely. Alternatively, modify your application architecture so that header.php only renders content when a view is actually requested, and returns early or uses conditional rendering based on whether a redirect has already been queued.
Using frameworks like Laravel provides built-in mechanisms to handle this gracefully through route closures or middleware, ensuring headers are managed centrally. For legacy PHP applications, the discipline of "no output before headers" remains paramount. Always verify your file encoding is set to UTF-8 without BOM (Byte Order Mark), as a hidden BOM at the start of a file can also trigger this warning on Windows servers or when using MAMP.
Finally, check for debugging statements or accidental echo calls within the included files. Developers often leave var_dump() or print_r() in place during development, which instantly corrupts header state. If you are using MAMP locally, ensure that your configuration does not force output buffering settings that interfere with immediate header manipulation, although standard PHP behavior usually dictates the strict "no output" rule regardless of buffer settings.
