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

Python contextlib for Timing Python code

If you've ever found yourself needing to measure the execution time of specific portions of your Python code, the `contextlib` module o...