Cannot modify header information, headers already sent: finding the line that caused it

The full message is “Warning: Cannot modify header information - headers already sent by (output started at /path/file.php:12)”. It means PHP has already started sending text to the browser, and after that it can no longer change the headers (redirect, start a session, set a cookie, deliver a file). What matters is inside the brackets: the file and line where text started coming out. That is where the culprit is.

Reading the message

Part of the message What it says
headers already sent by (output started at file:line) Where text started coming out. Start here.
... in another-file.php on line N Where the code tried to change the header (a header(), session_start() or setcookie()). It is the victim, not the culprit.

The causes, most common first

1 Spaces or blank lines outside the PHP. A blank line before <?php, or after a final ?>, is text sent out. It happens a lot in configuration files edited by hand, such as wp-config.php or a theme’s functions.php. The remedy is to delete the space and, in files that are pure PHP, stop using the closing ?>.
2 The “BOM”. Some editors (Notepad, for instance) save the file with three invisible bytes at the start. To catch them, in a Linux Terminal: grep -rl $'\xEF\xBB\xBF' --include=*.php .. Save the file again as “UTF-8 without BOM”.
3 An echo, print or HTML before the header(). In your own code, move the header change to the top, before any output.
4 A printed warning or error. If PHP writes a warning on screen, that text is already output and the later headers fail. Solve the original warning: see PHP warnings showing on your site: what they are and how to silence them.
5 A plugin or a theme. In WordPress, the message names the plugin’s or the theme’s file. Deactivate it and see whether it goes away. See disabling plugins, including when you cannot get in.

The “trick” that postpones the problem

An ob_start() at the top of the code keeps the output in memory and sends it only at the end, so headers can still be changed. It fixes the symptom, not the cause, and hides the blank space that is still there. Here PHP ships with output_buffering off, so any byte before the header gives the error. Use ob_start() as a last resort, in your own code, and not as a fix for a third party’s file.

Always edit a copy. A mistake in a wp-config.php or a functions.php leaves the site blank. Save a copy of the file before touching it. If the site goes blank, see a blank page in PHP: how to see the error.
Redirects and sessions are the usual victims. “Login does not redirect” or “sessions do not save” often start here. See PHP sessions: where they live, and why users get logged out.

Have the message with the file and line and cannot see what is there? Send us the full message and the page address.

Open a support ticket

SEE ALSO

PHP warnings showing on your site: what they are and how to silence them

PHP sessions: where they live, and why users get logged out

Where the PHP error log is

A blank page in PHP: how to see the error

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from $6.60/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?