PHP error: Cannot modify header information – headers already sent

PHP error: Cannot modify header information – headers already sent

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

Stefan Bogdanescu

Founder & Senior Architect

PHP error: Cannot modify header information – headers already sent

The "Cannot modify header information" error is one of the most persistent headaches in PHP development, particularly when dealing with session-based access control. You encounter it exactly where you least expect to: inside a conditional block meant to redirect unauthenticated users. The root cause isn't necessarily a logic flaw in your if statement, but rather the state of the output buffer at that specific moment.

In the scenario described, the issue stems from the order of execution relative to output generation. When you call include_once("header.php"), PHP attempts to execute every line within that file immediately. If header.php contains any HTML markup (like a banner), whitespace before the opening <html> tag, or even invisible characters like newlines, data is flushed to the browser. Once that first byte of output leaves the buffer, the headers are considered "sent."

This creates a conflict when you attempt to execute header('Location: index.php'). The HTTP protocol dictates that response headers must be transmitted before any body content. If your script has already echoed something—whether it's a banner image, a single space, or a newline character—the headers phase is effectively over. PHP then throws the fatal error because you are trying to modify state that is no longer accessible.

The specific code snippet provided highlights a common architectural misunderstanding regarding the flow of control:

include_once ("header.php");

if ($_SESSION['uid'] != 'programmer') {                        
    header('Location: index.php');
    echo 'you cannot be here';
    exit;
}   

Even though the error message suggests headers were already sent, the redirect logic itself is flawed. The line echo 'you cannot be here' will technically never execute if the redirect works correctly, but it signals a misunderstanding of control flow. More importantly, relying on header.php to output content before every authorization check is an anti-pattern for authentication logic.

To resolve this, you must ensure that no output is generated before the header() call. However, simply removing whitespace from header.php is often a temporary fix if the file includes other elements that might inadvertently trigger output. A more robust solution involves restructuring the inclusion order or using session checks exclusively on pages where headers can be safely modified.

If header.php is truly required for layout consistency across all pages, you cannot place authentication logic after including it if that logic requires a header redirect. Instead, you should verify the user's session status before including the header file. Move the authorization check to the very top of your protected page:

if (!isset($_SESSION['uid']) || $_SESSION['uid'] != 'programmer') {                        
    header('Location: index.php');
    exit; // Stop execution immediately
}

include_once ("header.php");
// The rest of the page logic goes here

By moving the check to the top, you guarantee that if the user lacks permission, the script terminates before header.php is even touched. This prevents any HTML or whitespace from flushing into the output buffer prior to the redirect command.

It is also worth noting that using echo immediately after a failed authentication check, as seen in your original code, is unnecessary and potentially dangerous. If the header function fails due to already-sent headers, PHP might skip the redirect attempt entirely depending on error reporting settings, leading to confusing "403 Forbidden" messages or exposed error logs. Always use exit (or die) immediately after a successful header redirect to prevent further execution.

For larger applications where this pattern recurs frequently, consider implementing a middleware-like approach or using a framework abstraction that handles session validation centrally before any layout rendering occurs. This keeps your logic clean and ensures the HTTP protocol contract is respected without constantly debugging whitespace issues in header.php.

Our network