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.
Milos
I have just descovered something very important. My controller was working just fine with all redirect etc. After I changed the controller file to utf8 and whenever I had a redirect or session command it gave me information already send by my controller file.
It is because I change it to utf8. I changed it back to ansi and it was all ok.
It is very hard to see this error because part of controller does work. Probably when notepad++ changed my file to ut8 it added some chars which are not readable to us, but are to the server.
Check this when you get an error
DaCoder
Awesome! I think this message is very common but some of the causes can be very obscure. Thanks for helping everyone on this one by posting your observation.
pidudiduuu
After months of Cake, i finally understood this error. thanks a lot! 😀
DaCoder
This is one of those errors that are so difficult to track why but once you do (after many hours and coffee) it makes so much sense. Thanks for your comment.
John S
Excellent site, thanks for the FREE info 🙂