PHP programmers commonly encounter the “Cannot Modify Header Information” error, which is normally displayed when extraneous white space is included in a PHP header file. When a programmer who is new to PHP encounters this error, he/she may become frustrated when trying to determine the problem’s source since the header portion of the programming that he/she has just completed normally generates it. The error can be displayed while working in any PHP-related content management system (CMS) or while writing hand crafted code.

What does the Cannot Modify Header Information Error Look Like?

The error’s specific context depends on whether the user is developing in WordPress, Drupal, or another PHP development environment. However, the error message will be pretty specific about the file and line number that the problem is occurring in. For example:

Warning: Cannot modify header information – headers already sent by (output started at …/wp-config.php:1) in wp-login.php on line 12

is a sample output error message.

The header has to be served first from a web server to a requesting client. Therefore, the error is displayed when some part of the web page’s body was sent while a request was made to set a header value.

How to Fix the Cannot Modify Header Information Error

Step 1 – Find the applicable “Header()” statement that is creating the problem. The error will be on or just before the line number reported in the error message.

Step 2 – Attempt to find any statements that could have sent output to the user before the header statement call. Complex conditional statements can sometimes complicate an attempt to locate the problem area. However, some programmers have found it useful to include one as early as possible in order to set the header value at the top of the PHP script to avoid the issue.

Step 3 – Ensure that there is no white space prior to the PHP start and end tags. This will be the most common cause of the error for new PHP programmers since a blank line at the top of the PHP script would probably be ignored (as with other programming languages). In PHP, this white space is interpreted as an “echo” statement which will print out a blank line and cause the error to be displayed.

Step 4 – Ensure that there are no extraneous spaces or new line entries on or before the line number of the error message if the error is still being displayed while the code is being troubleshooted. Some Windows based text editors add this white space when editing PHP files without the user’s knowledge.

Step 5 – Save all work and reload the PHP driven page and the error will be corrected.