What the message looks like
PHP Warning: Cannot modify header information - headers already sent by (output started at /var/www/example.com/config.php:1) in /var/www/example.com/login.php on line 12Common causes
- A UTF-8 BOM (hidden bytes) at the start of a PHP file, often saved by an editor, usually in
config.phpor a template. - Whitespace or a blank line before
<?phpor after a closing?>in an included file. echo,printor HTML output beforeheader(),setcookie()orsession_start().- A PHP warning or notice printed to the page before the header call (
display_errorson), which then causes this second error. - A plugin or theme in a CMS that prints output early.
Check
- Find the file named in the message and check its first bytes for a BOM
head -c 3 /var/www/example.com/config.php | xxd - Search PHP files for a BOM
grep -rl $'\xEF\xBB\xBF' --include='*.php' /var/www/example.com | head - Is there output after a closing tag at the end of the file?
tail -c 30 /var/www/example.com/config.php | xxd - Check the file for syntax problems
php -l /var/www/example.com/config.php
Fix
- Open the file the message names and remove everything before
<?php, including the BOM (save as UTF-8 without BOM). - In files that contain only PHP, leave out the closing
?>so no trailing newline is sent. - Move
header(),setcookie()andsession_start()above any output, or restructure so output happens last. - If an earlier warning is the first output, fix it or set
display_errors = Offin production and log errors instead. - As a stopgap only,
ob_start();at the top buffers output, but fix the real cause.
FAQ
What does output started at config.php:1 mean?
The first byte was sent from line 1 of that file. That points to a BOM or whitespace before the opening tag.
Is it safe to ignore the warning?
No. The redirect, cookie or session that should have been set is lost, so logins and redirects can silently fail.