How to fix "Headers already sent" error in PHP
When you run a script and encounter a Warning: Cannot modify header information error, the message explicitly tells you where the problem lies. The warning usually points to a specific file and line number where output began, distinct from the line where you attempted to call header() or setcookie(). In the scenario described, the error indicates that output started at /some/file.php:12, but the header modification was attempted at line 23.
This happens because PHP buffers all output streamsâHTML, whitespace, newlines, and even BOM charactersâbefore sending headers to the client. Once any byte is sent to the browser, the headers are considered "sent." At that point, PHP locks the stream for security reasons to prevent race conditions or malformed HTTP requests. You cannot then inject a Content-Type, redirect the user, or set a cookie without violating the HTTP protocol structure.
The most common culprit is invisible whitespace. Developers often place an opening <html> tag, a BOM from a text editor, or even a single space after the PHP opening tag <?php before any logic executes. If line 12 contains a stray newline or indentation character before the first executable code, that byte is sent immediately. When you reach line 23 to call header(), PHP throws the warning because the stream is already in output mode.
To diagnose this, inspect the file pointed to by the error message using a hex editor or a binary-aware text viewer. Look for non-printable characters before your first logic block. You can also enable display_errors and check if the error log reveals exactly which character triggered the buffer flush. Sometimes, including files with whitespace issues at the top of your script is the root cause. If you include a partial file that ends with a newline, that newline counts as output.
Fixing the issue often involves strict adherence to ordering. Ensure that all require_once or include statements happen before any echo, print, or whitespace. However, this is not always a viable trade-off if your bootstrap logic requires immediate output (e.g., legacy scripts). In such cases, you must buffer the output using ob_start() before executing code that generates content. This allows you to capture the output stream and manipulate headers later, provided no actual data has been flushed yet.
Another frequent source of this error is mixing static assets with dynamic PHP logic. If your application serves images or CSS files directly from a folder without proper MIME type configuration, or if an image contains metadata that triggers an error before the header is set, you might encounter this issue indirectly. Ensure your server configuration correctly handles static files so they bypass PHP entirely.
In larger frameworks like Laravel, this error is less common due to robust scaffolding, but it persists in custom controllers or legacy integrations. If you are working on a project where you cannot easily refactor the code order, consider using output buffering wrappers to delay the actual transmission until headers are ready. However, be aware that excessive buffering can consume memory and introduce latency if not managed correctly.
Ultimately, the fix is structural. You must guarantee that no characters, visible or invisible, leave the script before the header functions are invoked. This discipline prevents subtle bugs where a seemingly harmless space character breaks critical redirect logic or security headers.
