Warning: Cannot modify header information - headers already sent
Warning: Cannot modify header information - headers already sent by (output started at /var/www/app/config.php:1) in /var/www/app/login.php on line 14Short answer
PHP sent output to the browser before your header(), setcookie() or session_start() call, and headers must go first. The warning ends with 'output started at file:line': open that file at that line. Remove the stray whitespace, echo or warning there, or buffer output with ob_start().
Fix it fast
1Find out which cause you have
Read the part in brackets: output started at <file>:<line>. That is where the first byte was sent, not where the header call is. Check it directly, or ask PHP in code.
head -c 40 config.php | cat -A // in code, before header(): if (headers_sent($file, $line)) { error_log("$file:$line"); }2Match what you see, then jump to its fix
- 1
output started at ...:1, or on the last line of an included fileWhitespace or text outside the PHP tagsFix - 2
output started at the same file and a line near the top of the failing scriptAn echo, HTML or a PHP warning printed before the header callFix - 3
Templates or plugins echo before your controller can set headersOutput has to happen early and cannot be moved, so buffer itFix
- 1
What does the error look like?
Warning: Cannot modify header information - headers already sent by (output started at /var/www/app/config.php:1) in /var/www/app/login.php on line 14Warning: session_start(): Session cannot be started after headers have already been sent
Cause 1 of 3
Whitespace or text outside the PHP tags
1Check
A blank line before <?php, or a newline after a closing ?> at the end of an included file, is output. The manual's rule is that header() must be called before any output, including blank lines in a file.
head -c 20 config.php | cat -A
tail -c 20 config.php | cat -A2Fix
Delete anything before <?php. In files that contain only PHP, leave out the closing ?> tag so no trailing newline can slip in.
<?php
// config.php: no blank line above, no closing tag below
$dbHost = 'localhost';3Verify
Reload; the warning is gone and the redirect or cookie works.
curl -i http://localhost/login.php | head -n 12Confirmed by php.net
Cause 2 of 3
An echo, HTML or a PHP warning printed before the header call
1Check
Debug echo and var_dump calls, HTML above the PHP block, and even a PHP Warning or Notice printed to the page all count as output.
grep -n -E "echo|print|var_dump|<html" login.php | head2Fix
Move the header(), setcookie() or session_start() call above any output, or fix the warning that is printing text. Call exit after a redirect.
<?php
session_start();
if (!isset($_SESSION['user'])) {
header('Location: /login');
exit;
}
?>
<html>3Verify
Request the page with curl and see a Location header.
curl -i http://localhost/dashboard.php | head -n 5Confirmed by php.net
Cause 3 of 3
Output has to happen early and cannot be moved, so buffer it
1Check
Output buffering holds output in memory until the script ends or the buffer is flushed, so headers can still change. The cost is that the whole response waits in memory.
php -i | grep output_buffering2Fix
Start a buffer at the top of the entry script, or set output_buffering in php.ini. Moving the header call earlier is better when you can.
<?php
ob_start();
// ... code that may echo ...
header('Location: /done');
ob_end_flush();3Verify
The redirect works and no warning is logged.
curl -i http://localhost/page.php | head -n 5Confirmed by php.net
How do I stop it coming back?
- Leave out the closing ?> in files that contain only PHP.
- Send headers, cookies and session_start() at the very top of the request, before any template.
- Keep display_errors off on production, so a warning never becomes page output.
- Use a PSR-12 style check in CI to catch leading whitespace.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
PHP shows a blank page and no error message
No Warning or Fatal error text, even though the script is broken
A blank page means PHP hit an error but was told not to print it.
4 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix