"Headers Already Sent" in PHP: How to Find and Fix the Cause
Quick answer: "Headers already sent" means PHP had already sent some output to the browser before a call to header(), setcookie() or session_start(). The message tells you the file and line where that output started: usually whitespace before <?php, a blank line after ?>, a UTF-8 BOM, a forgotten echo or a warning. Remove the output at that point, and don't close pure PHP files with ?>.
The full message looks like this:
Warning: Cannot modify header information - headers already sent by
(output started at /var/www/site/config.php:1) in /var/www/site/login.php on line 14
It is one of the most common PHP errors, and it breaks redirects, logins, sessions, cookies and file downloads. The good news: the message already tells you where to look.
Why it happens
An HTTP response has two parts: the headers first (status code, redirects, cookies, content type), then the body (the HTML or JSON). As soon as PHP sends the first byte of the body, the headers are sent along with it and can't be changed anymore.
So any output, even a single space or an invisible character, sent before header(), setcookie(), session_start() or http_response_code() triggers the error.
How to read the message
The message has two locations, and the important one is the first:
- "output started at config.php:1": where the output came from. This is what you fix;
- "in login.php on line 14": where the header was attempted. This line is usually fine.
Line 1 or the last line of a file almost always means whitespace, a BOM or a closing tag. Any other line usually points to an echo, HTML or a warning.
The 6 most common causes
1. Whitespace or HTML before <?php
A blank line or space before the opening tag is sent as output:
<?php // the space before <?php is output
header('Location: /dashboard');
The opening tag must be the very first thing in the file.
2. Whitespace after a closing ?>
A newline after ?> at the end of an included file is output too, and it is invisible in most editors. In files that contain only PHP, leave out the closing tag. PSR-12 recommends exactly this.
3. UTF-8 BOM
Some editors save files as "UTF-8 with BOM", adding three invisible bytes (EF BB BF) at the start. PHP sends them as output, and the error points to line 1. Save the file as UTF-8 without BOM: in VS Code, click the encoding in the status bar and choose "Save with Encoding > UTF-8"; in Notepad++, Encoding > UTF-8. To find files with a BOM on Linux:
grep -rlI $'^\xEF\xBB\xBF' --include='*.php' .
4. echo, print, var_dump or HTML before the header
<?php
echo 'Saved'; // output
header('Location: /dashboard'); // too late
exit;
Decide where to redirect before printing anything:
<?php
header('Location: /dashboard');
exit;
Always call exit after a redirect header, or the rest of the script keeps running.
5. Warnings and notices displayed on screen
With display_errors on, a warning such as "Undefined array key" is printed as HTML, which counts as output. The real fix is the warning itself; in production, also turn off display_errors and log errors instead. See how to find PHP errors in the logs.
6. An include that prints something
<?php
include 'header.php'; // prints the page header HTML
header('Location: /login'); // too late
exit;
Do all the logic first (authentication, redirects, cookies, sessions), and only then include templates:
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: /login');
exit;
}
include 'header.php';
Sessions and cookies send headers too
session_start() sends a cookie header, and so does setcookie(). Call them at the top of the script, before any output:
<?php
session_start();
setcookie('theme', 'dark', [
'expires' => time() + 86400 * 30,
'path' => '/',
'secure' => true,
'httponly' => true,
'samesite' => 'Lax',
]);
Checking in code with headers_sent()
When debugging, headers_sent() tells you whether output has started and where:
<?php
if (headers_sent($file, $line)) {
error_log("Output already started in $file on line $line");
}
Output buffering: a temporary workaround
ob_start() holds output in memory so headers can still be sent later:
<?php
ob_start();
// ... code that may print something ...
header('Location: /home');
exit;
Some servers enable it globally through output_buffering in php.ini, which is why the same code works on one server and fails on another. Treat buffering as a band-aid: the stray output is still there, and in a JSON API it would corrupt the response. Fix the real cause.
Headers already sent in a JSON API
In APIs, the same stray output that breaks header() also breaks the JSON: a BOM or a warning before { makes the client fail to parse it. Send headers first, print the JSON once at the end, and never echo debugging information. See error handling in a PHP API.
Quick checklist
- Nothing before
<?phpin any file involved; - Files saved as UTF-8 without BOM;
- No closing
?>in files that are only PHP; - No
echo,printorvar_dumpbeforeheader(); session_start()andsetcookie()at the top;- Logic and redirects before including templates;
display_errorsoff in production;exitafter every redirect.
Conclusion
"Headers already sent" always has the same root cause: output before a header. The "output started at" part of the message points to the exact file and line, and the fix is usually removing a space, a BOM, a closing tag or an echo there. For other common PHP errors, see 403 Forbidden on PHP sites.
Frequently asked questions
Why does it work on my computer but not on the server?
Your local PHP probably has output_buffering enabled, which hides the problem. The server doesn't, so the stray output shows up.
Should I just use ob_start() everywhere?
No. It hides the symptom, but the stray output can still break JSON responses, downloads and images. Find and remove it.
The error points to line 1 and I see nothing there. Why?
It is almost certainly a UTF-8 BOM or whitespace before <?php. Re-save the file as UTF-8 without BOM.
Do I need ?> at the end of PHP files?
No. Leaving it out in files that contain only PHP is recommended, because it prevents accidental output.
Can JavaScript redirects replace header('Location')?
They work, but they are slower, depend on JavaScript, and don't send a proper HTTP redirect. Fix the output and use a real redirect.