Error & Fix PHP Development PHP 8.3

How to Fix "Headers Already Sent" Error in PHP

PHP says "Cannot modify header information - headers already sent" when output reaches the browser before your header() call. Here is how to read the message, find the exact line that sent output, and fix it for good.

RH Raja HunainAuthor 22 Nov 2026 2 min read 26 views Updated 23 Nov 2026
On this page
Share

Explanation

You call header('Location: dashboard.php'), the page stays where it is, and PHP prints this warning instead:

Warning: Cannot modify header information - headers already sent by
(output started at /var/www/app/login.php:2) in /var/www/app/login.php on line 9

Nothing crashes โ€” the script keeps running โ€” but the redirect never happens, the cookie is never set, and the session never starts. It appears in login forms, redirects, file downloads and session_start() calls, and the cause is almost never the line the warning points at.

What the Error Actually Means

An HTTP response has two parts, sent in a strict order: headers first, then the body. Headers carry the status code, the redirect address, cookies and the content type. The body is the HTML the browser shows.

PHP sends the headers the moment the first byte of body output leaves the script. After that, the headers are gone โ€” sealed and delivered. Any later call to header(), setcookie(), session_start() or header_remove() has nowhere to go, so PHP warns and drops it.

That is the whole rule: no output before any header function.

How to Read the Error Message

The warning is unusually helpful. It names two places:

Warning: Cannot modify header information - headers already sent by
(output started at /var/www/app/login.php:2) in /var/www/app/login.php on line 9
  • "output started at login.php:2" โ€” this is the line that matters. Something on line 2 of login.php sent the first byte of output. That is where your bug lives.
  • "in login.php on line 9" โ€” this is only the victim: the header() call that arrived too late. Do not "fix" this line. Fix line 2.

Open the file named in the first half and look at that exact line.

Fix 1: Whitespace Before <?php or After ?>

The most common cause is invisible output: a blank line or a space before the opening tag, or a newline after the closing tag.

<!-- a blank line here is output -->
<?php
header('Location: dashboard.php');

A single space before <?php counts as output, and so does the newline after a closing ?> at the end of an included file. The fix is simple: make <?php the very first characters of the file, with nothing โ€” not even a blank line โ€” before it.

For files that contain only PHP, drop the closing tag entirely. PHP's own style guidance says so, and it removes a whole class of accidents:

<?php
// config.php โ€” no closing tag at the end
$dbHost = 'localhost';

If the warning says "output started at ... on line 1", this is almost certainly your cause: leading whitespace, or a UTF-8 byte order mark (see Fix 4).

Fix 2: Echo, Print or HTML Before the Header Call

Any deliberate output before the header call breaks it โ€” echo, print, print_r, var_dump, or raw HTML sitting outside the PHP tags:

<?php
echo 'Welcome back';
header('Location: dashboard.php'); // Warning โ€” the echo already sent output

The fix is to move the header logic above all output. Process first, display last:

<?php
if ($loggedIn) {
    header('Location: dashboard.php');
    exit;
}
echo 'Welcome back';

Notice the exit after header(). A redirect header does not stop the script โ€” without exit, the code below keeps running. Always exit right after a redirect.

Fix 3: An Included File That Sends Output

The output often comes from a file you included, not the file you are looking at. If config.php ends with a stray newline after ?>, or a helper file prints a debug line, that output counts against your script:

<?php
require 'config.php'; // a blank line after ?> in this file is output
header('Location: dashboard.php'); // Warning

Check every file in the include chain for the same problems: whitespace before <?php, text after ?>, and leftover echo or var_dump calls from debugging.

Fix 4: The UTF-8 Byte Order Mark (BOM)

The BOM is three invisible bytes (EF BB BF) some editors add at the very start of a file to mark it as UTF-8. PHP treats those bytes as output, so the warning points at line 1 even though the file looks empty before <?php.

Open the file in your editor and save it as "UTF-8 without BOM" (in VS Code: click the encoding in the status bar, choose "Save with Encoding", then "UTF-8"). Then check every included file the same way โ€” one BOM anywhere in the chain is enough to break headers.

Fix 5: session_start() and setcookie() After Output

session_start() sends a cookie header, and setcookie() is a header function too, so both follow the same rule. This fails:

<html>
<?php session_start(); ?>

Move the session start to the very top of the script, before any HTML:

<?php
session_start();
?>
<!DOCTYPE html>
<html>

The same applies to setcookie() and header('Content-Type: ...') โ€” all header work happens before the first byte of HTML.

The Quick Fix: Output Buffering

If restructuring is not practical right now, output buffering holds the output in memory instead of sending it, so header calls keep working until the buffer is flushed:

<?php
ob_start();
echo 'This stays in the buffer';
header('Location: dashboard.php');
ob_end_flush();

Use this as a safety net, not as the cure. Buffering hides the early output instead of removing it, and on a large page it holds the whole response in memory. Fix the real cause with the steps above, and keep ob_start() for the cases where output genuinely cannot be reordered โ€” a legacy template, for example.

How to Find the Source When the Message Is Not Enough

When the "output started at" note points somewhere unhelpful, use headers_sent() to test whether headers are already gone, and where they went:

if (headers_sent($file, $line)) {
    echo "Headers were already sent in $file on line $line";
}

This is handy inside a framework or a long include chain: drop the check just before your header() call and it tells you the file and line that sent output. Also turn on error display while debugging, so PHP shows the warning with the full file paths:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Common Causes at a Glance

Cause Symptom in the message Fix
Blank line or space before <?php Output started at line 1 Remove it; <?php must be first
Newline after ?> in an included file Output started in the included file Omit the closing tag in PHP-only files
echo / HTML before header() Output started at the echo line Move header logic above all output
UTF-8 BOM Output started at line 1, file looks clean Re-save as UTF-8 without BOM
session_start() after HTML Output started at the HTML line Start the session at the top
Debug var_dump() left in an include Output started in the included file Remove it, or buffer output
Error or notice printed before headers Output started at the error line Fix the underlying error first

Note the last row: an earlier PHP notice is itself output. If a warning prints before your header() call, it triggers this error too โ€” fix the first error and this one often vanishes with it.

Frequently Asked Questions

Why does the redirect work on one server and fail on another? The other server probably has output buffering enabled in php.ini, which hides the early output. The bug exists on both. Fix the output, not the server setting.

Should I just add ob_start() at the top of every file? It will silence the warning, but it hides real output-order bugs and holds responses in memory. Use it where restructuring is genuinely hard, and fix the cause everywhere else.

Does this error happen in Laravel? Rarely, because the framework buffers responses for you. When it does appear, something echoed output before Laravel built the response โ€” a stray echo, a var_dump, or a BOM in a config file. The same "output started at" line tells you where.

Why does session_start() fail but echo works fine? echo is body output โ€” it is allowed any time. session_start() sends a cookie header, and headers must come before the body. The order is the whole problem.

Conclusion

The "headers already sent" error is PHP enforcing one rule: headers before body. Read the "output started at" half of the warning โ€” that file and line is the bug, not the header() call. Remove the whitespace before <?php, drop the closing tag in PHP-only files, move header logic above all output, strip the BOM, and start sessions at the top. Then the redirect, cookie or session that was silently failing starts working again.

Was this helpful? 0 likes Login to like & bookmark
RH

Written by

Raja Hunain

Full Stack Developer at Raja Dev Solutions โ€” writes about real development work, the errors he faced and exactly how he fixed them, plus practical Laravel, PHP, JavaScript and MySQL tutorials.

More about the author

Comments (0)

No comments yet. Be the first to comment.

Leave a comment

Comments are moderated and appear after approval.

Keep reading

Related Errors & Articles