I got the following error message while testing my CakePHP web application, specifically a new controller that was redirecting to another controller on an error. Sometimes error messages will be cryptic or just a “catch-all” for some kind of other issue that went wrong but was not addressed correctly, but this time the error message was correct, my application was trying to redirect when the headers were already sent, so let’s fixed that.
This is the full error message I got:
Warning (2): Cannot modify header information – headers already sent by (output started at /XXXXX/XXXXX/XXXXX/XXXX/MyDomainName/MySubdirectoryToApp/cake/basics.php:110) [CORE/cake/libs/controller/controller.php, line 744]
Notice how it points to a file “basics.php” in cake instead of your own files.
The most common solution for this issue is the characters after the closing “?>”, but not in this case, mostly because is pointing to a file other than your own files.
In basics.php, line 110, you will find the code for the function debug(). And that was the first error, I was still using the debug function to check the information returned from the Db even after adding the redirect. Once removed, the error was different. Same “Warning (2): Cannot modify header information – headers already sent by…” but this time it was not the debug function but some echo functions I was using for debugging other parts of the controller.
If you get the message “Warning (2): Cannot modify header information – headers already sent by” and already checked that there is no extra characters at the end of your PHP script, check for DEBUG and ECHO functions, as those will send data to the screen and be a cause for the above mentioned error message.