Running into the “Cannot modify header information – headers already sent” error in PHP? Don’t worry—you’re not alone. This is one of the most common errors PHP developers face, especially when working with things like page redirects or cookies. The frustrating part is how unclear the message can be at first. The good part? It’s usually an easy fix once you know what to look for. In this guide, we’ll break down what causes this error, how to fix it, and how to avoid it moving forward.
Introduction
If you’ve ever worked with redirects, file downloads, or cookies in PHP, you’ve probably come across the function header()
. This function is essential for sending raw HTTP headers to the browser—things like redirecting a user to another page (header("Location: page.php")
), setting content types, or controlling caching behavior. However, there’s a strict rule you must follow when using header()
: you cannot send any actual output to the browser before calling it.
What does “output” mean in PHP? It includes:
echo
,print
,var_dump()
, etc.- HTML code outside of PHP tags
- Even a single space, newline, or invisible character before
<?php
or after?>
The moment PHP encounters any of these outputs, it sends the HTTP headers automatically. Once headers are sent, it’s too late to modify them, and calling header()
afterward will trigger this warning:
Warning: Cannot modify header information - headers already sent by (output started at /path/to/file.php:line)
This error can be especially confusing because it often points to a completely different line than the one with the actual issue—sometimes even in an included file. That’s why many developers struggle with it at first.
The good news? Once you understand why this error happens and how headers work in PHP, it’s actually easy to fix. In this blog post, we’ll go over the most common causes of this error, how to fix it step by step, and how to make sure it doesn’t come back to haunt you in the future.
Common Causes of the Error
This error is almost always caused by one of these culprits:
- Whitespace Before
<?php
or After?>
: This is the most common and often the trickiest to spot. An accidental space, newline, or even a Byte Order Mark (BOM) at the very beginning of your PHP file (before the opening<?php
tag) or after the closing?>
tag will be sent as output. echo
orprint
Statements Before Headers: If youecho
orprint
anything (even an empty string) before yourheader()
calls, cookies, or session starts.- HTML Before PHP Headers: Any HTML content (including DOCTYPE declarations,
<html>
tags, or even comments) that appears before your PHP header-modifying functions. - Error Messages: If an error occurs before your header calls and PHP displays the error message, that counts as output.
print_r()
,var_dump()
,var_export()
for Debugging: While incredibly useful, using these functions before you’re done with header modifications will cause the error.- Included/Required Files: If a file you’re including or requiring has any of the above issues.
How to Fix It (Step-by-Step)
Let’s get down to business! Here’s a systematic approach to hunting down and squashing this bug:
Check for Whitespace:
- Beginning of file: Open your PHP file and carefully check for any characters (spaces, tabs, newlines) before the
<?php
tag. Delete them. - End of file: If you have a closing
?>
tag, check for any characters after it. Often, it’s best practice to omit the closing?>
tag in files that only contain PHP code to prevent accidental whitespace. - Use a good editor: Many modern code editors can highlight invisible characters or even show BOMs.
Relocate Header-Modifying Functions:
- Move to the top: Ensure all your
header()
,setcookie()
,session_start()
, andsession_regenerate_id()
calls are at the very beginning of your PHP script, before anyecho
,print
, or HTML output.
Bad:
<?php
//Bad example
echo "Hello!"; // Output sent here!
header('Location: success.php'); // Error!
exit();
?>
Good:
<?php
session_start(); // Good! At the very top.
header('Location: success.php'); // Good! Before any output.
exit(); // Always exit after a redirect!
?>
Inspect Included/Required Files:
- This is crucial! If your main file is clean, check all the files it includes or requires for the same issues (whitespace, premature output). Recursively check any files they might include as well.
Temporary Debugging Output:
- If you’re using
echo
,print_r()
,var_dump()
, orvar_export()
for debugging, make sure they are removed or commented out before any header-related functions.
Error Reporting:
- Temporarily set
error_reporting(E_ALL);
andini_set('display_errors', 1);
at the very beginning of your main script (beforesession_start()
if you have it) to catch any hidden errors that might be causing output. Remember to remove or disable these for production.
Test the Fix
Once you’ve made your changes:
- Clear your browser cache.
- Restart your server (especially on local environments like XAMPP, WAMP).
- Reload your PHP file in the browser.
- Use tools like
headers_list()
to debug which headers are being sent.
header("Location: home.php");
print_r(headers_list()); // Useful for debugging
exit();
Pro Tips
- Always use
exit()
afterheader("Location: ...")
to prevent further script execution. - Use output buffering with
ob_start()
at the beginning of scripts when unsure. - Avoid mixing HTML and PHP in the same file if possible.
- Use
error_reporting(E_ALL)
andini_set('display_errors', 1)
during development to catch output issues early.
Related Errors or Alternative Scenarios
- “Headers already sent by…” with a specific file and line number: This is a golden clue! It tells you exactly where the offending output originated. Go directly to that file and line number to investigate.
- Trying to set a cookie after
echo
: Same principle asheader()
. Cookies are sent as headers. session_start()
error:session_start()
also sends aSet-Cookie
header for the session ID. If you get this error, it’s the same problem – premature output.
Conclusion
The “Cannot modify header information” error can seem tricky, but once you understand how PHP headers work, it becomes easy to avoid. Always remember: headers must be sent before any actual output. By keeping your code clean, minimizing whitespace, and using output buffering when needed, you’ll steer clear of this issue.
Happy coding!