Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Warning: Cannot modify header information - headers already sent

ErrorWarning: 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 14

Short 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().

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"); }
  2. 2Match what you see, then jump to its fix

    1. 1output started at ...:1, or on the last line of an included fileWhitespace or text outside the PHP tagsFix
    2. 2output 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. 3Templates or plugins echo before your controller can set headersOutput has to happen early and cannot be moved, so buffer itFix

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 14
  • Warning: 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 -A

2Fix

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 12

Confirmed by php.net

Did this fix it?

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 | head

2Fix

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 5

Confirmed by php.net

Did this fix it?

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_buffering

2Fix

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 5

Confirmed by php.net

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.