Friday, March 26, 2010

Fixing Warning (2): Cannot modify header information - headers already sent by (output started at...

How to fix the following error in PHP
Warning (2): Cannot modify header information - headers already sent by (output started at  ... 

This is also referred to as "whitespace problem".
The error message typically looks something like
Warning (2): Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/test-cake/app/models/post.php:1) [CORE/cake/libs/controller/controller.php, line 644]

Steps to resolve the issue:
1) Find the header() statement that is causing the problem. The error must be at or before this line.

2) Identify statements that could send output to the user before this header statement. Example 'echo ' statements. You cannot have these 'echo' statements before header() statement.

3) Make sure there is no white space before the php start and end tags.
Also at the beginning of the file a blank line before the <?php start tag may look innocent, when processed by PHP, it will turn into an echo statement printing out a blank line. This is a common culprit.

No comments:

Post a Comment