"Headers already sent" in PHP — What It Means and How to Fix It
What the PHP error "Headers already sent" means and how to fix it.
Published October 31, 2026
PHP tried to send an HTTP header (redirect, cookie, session) after output had already begun. Headers must go before any body output.
What it means and why it happens
Common causes: whitespace before <?php, an echo or HTML before header()/session_start(), a BOM in a UTF-8 file or a closing ?> followed by blank lines.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
Warning: Cannot modify header information - headers already sent by (output started at index.php:2)
Practical examples
Code that triggers it
<?php
echo 'Hello';
header('Location: /home'); // too lateThe fix
<?php
header('Location: /home');
exit;
// no output before this pointBuffer output as a fallback
ob_start(); // at the very top
// ... later
header('Location: /home');
ob_end_flush();Common mistakes
- A blank line before <?php.
- Closing ?> tags with trailing spaces.
- Saving files with a BOM.
Key takeaways
- Headers already sent: PHP tried to send an HTTP header (redirect, cookie, session) after output had already begun.
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
Where did output start?
The message says 'output started at file:line'.
How do I find which line caused Headers already sent?
The error message and stack trace show the file and line; start there and work backwards.