PHP "headers already sent": how to find the output before your header()

PHP "headers already sent": how to find the output before your header()

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Warning: Cannot modify header information - headers already sent by (output started at /var/www/app/config.php:7) is PHP telling you two things: an HTTP header arrived too late, and exactly which file and line printed the first byte of output. Most developers stare at the file that calls header(); the bug is always in the file named after "output started at".

Why headers must come first

HTTP responses send all headers before the body. The moment PHP prints a single byte - a space, a newline, a BOM, an echo - the web server flushes the headers it has and starts the body. Any header(), setcookie() or session_start() after that point is too late.

Read the message literally

Warning: Cannot modify header information - headers already sent by
(output started at /var/www/app/includes/config.php:7)
in /var/www/app/login.php on line 23

login.php:23 is where the header() call failed. config.php:7 is where output began - that is the file to fix. If the message has no "output started at" part, output buffering hid the source; the usual suspects below still apply.

The four usual suspects

1. Whitespace outside PHP tags. A blank line or space before <?php, or after a closing ?> in an included file, is output. Line 7 of the included file often turns out to be an empty line after ?>. The fix that prevents the whole class: in pure-PHP files, omit the closing ?> tag entirely. PSR-12 requires it, and every framework follows it.

2. A UTF-8 BOM. Editors that save "UTF-8 with BOM" write three invisible bytes before <?php. The file looks clean and still emits output on line 1. Detect it:

head -c 3 config.php | xxd
# 00000000: efbb bf                                  <- BOM present

Re-save as "UTF-8" (no BOM) or strip it with your editor or sed -i '1s/^\xEF\xBB\xBF//' config.php.

3. Deliberate output before the redirect. A stray echo, var_dump, print_r or an HTML block above the logic that calls header('Location: ...'). Debugging leftovers are the classic offender.

4. An error or warning itself. A notice printed before your header() call is output too. The displayed warning text starts the body. Fix the underlying notice, not just the header error - and never rely on suppressed errors in production.

output_buffering: the band-aid, not the fix

Enabling output_buffering in php.ini or calling ob_start() holds output in memory so headers can be sent "later". Framework front controllers already buffer, which is why this error is rare inside Laravel or Symfony - but when you see it there, the source is almost always raw output in a service provider, middleware, or a dd()/echo before the response is returned. Buffering buys you time; it does not remove the stray byte. Find and delete the output.

A fast way to hunt it down

When the message is ambiguous, bisect: add exit('here'); halfway through the included files. If the warning survives, the output is earlier; if it changes, it is later. Two or three rounds isolate the file. On a codebase with many includes, grep -rn "?>$" --include='*.php' app config finds closing tags worth removing.

Summary

The warning names the file and line where output started - fix that, not the header() call. Omit closing ?> tags, save files as UTF-8 without BOM, remove stray echo and debug calls, and treat output buffering as a safety net rather than a solution.

PHP Errors HTTP Troubleshooting

Our network