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.
Typical scenario
You edited wp-config.php in a Windows editor and saved it as UTF-8 with BOM. Since then login redirects in WordPress fail with "Cannot modify header information – headers already sent by (output started at /var/www/html/wp-config.php:1)". The first three invisible bytes of the file were already sent as output. Check with head -c 3 wp-config.php | xxd whether efbbbf is there.
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.
How do I find all files with a BOM in a project?
grep -rl $'\xEF\xBB\xBF' --include='*.php' . lists every PHP file that starts with the BOM. Then re-save each as "UTF-8 without BOM" in your editor. Modern editors have a setting for this that you should enable permanently.
Can I work around the problem with output buffering?
Yes, with output_buffering in php.ini or ob_start() at the start. Output is then buffered and headers still go out before the body. That only hides the cause, though; invisible characters stay in the code and the problem returns under other settings.
Why does it only happen on the server and not locally?
The environments often differ in output_buffering. If buffering is active locally, a short piece of output goes unnoticed. On the server it is off or too small, and the headers are sent immediately. So fix the file, not the configuration.