setcookie, Cannot modify header information - headers already sent

setcookie, Cannot modify header information - headers already sent

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

Stefan Bogdanescu

Founder & Senior Architect

The Invisible Buffer: Why Headers Must Come First

The error Cannot modify header information - headers already sent is one of the most common stumbling blocks for developers new to PHP. It usually stems from a fundamental misunderstanding of how HTTP responses are constructed, specifically regarding the order of operations between your server and the client's browser. When you encounter this warning in XAMPP or any PHP environment, the server is explicitly telling you that it has already begun sending data before attempting to modify header properties like cookies.

In the scenario provided, the error message pinpoints the issue precisely: output started at C:\xampp\htdocs\test\index.php:9. This corresponds to the line where the HTML <body> tag begins. In PHP, headers—including those required for setting cookies with setcookie()—are sent to the browser immediately before the first byte of any body content is transmitted. Once the HTML output buffer receives even a single character, such as the opening bracket of a < tag or a newline character, the HTTP headers are considered "sent." At that moment, PHP locks the header array. If your code subsequently attempts to execute setcookie(), which requires modifying these headers, the server halts execution and throws the fatal error seen in the log.

The root cause in the provided example is not the setcookie() function itself, but rather the presence of HTML markup preceding it. The script structure looks like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        ...
    </head>
    <body> <!-- Header sent here -->
     <?php
     $value = 'something from somewhere';
     setcookie("TestCookie", $value); // Error: Cannot modify headers now
     ?>
    </body>
</html>

The browser receives the <!DOCTYPE declaration, processes it, and renders the page. By the time the PHP engine reaches line 9 inside the <body> tag, the opportunity to set cookies is lost. To fix this, all PHP logic that modifies headers must occur before any HTML is echoed or printed. The corrected structure should move the cookie setting logic to the very top of the file:

<?php
$value = 'something from somewhere';
setcookie("TestCookie", $value);
// Now it is safe to output HTML
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        ...
    </head>
    <body>
        <!-- Content goes here -->
    </body>
</html>

It is also worth noting that whitespace matters significantly in this context. In many PHP configurations, a stray newline character or even a space before the <?php tag counts as output. If you accidentally indent your opening PHP tag with spaces in your code editor, those spaces are sent to the browser before the script starts processing, triggering the same header error. Always ensure your file begins strictly with <?php.

When building more complex applications where multiple files might need to set cookies or redirect users, this limitation becomes a critical architectural constraint. Modern frameworks like Laravel address this by separating logic from views and managing output buffering automatically, effectively abstracting away these low-level HTTP constraints. For developers working outside of such frameworks, the rule remains absolute: execute header-modifying code before printing anything. If you find yourself needing to set a cookie after an include statement or a redirect check has already occurred, consider using ob_start() at the very beginning of your script to buffer output, though relying on strict ordering is generally more robust and easier to debug.

Our network