CakePHP - Warning - Cannot modify header information - headers already sent

CakePHP FAQ: Help, I'm getting a CakePHP error message like "Cannot modify header information - headers already sent"; what's going on? (Oh, and I may have just added some "redirect" functionality to my application.)

This PHP error message drives me crazy. The problem is that you have whitespace after a closing "?>" tag in one of your PHP files. Since I'm currently working with CakePHP, this means I have whitespace in a CakePHP model or controller file (possibly a CakePHP view file too, but I'm not sure about that).

CakePHP - cannot modify header information headers already sent (discussion)

In my case, I was working on a CakePHP controller named ProjectsController (projects_controller.php), and adding a few lines of whitespace after my closing "?>" tag in my PHP file. As soon as I did this, I started getting this PHP/CakePHP error "Cannot modify header information - headers already sent" message. As soon as I removed those blank lines, everything started working fine again.

The reason I added the lines of whitespace was to get my text editor (TextMate) to scroll a little more than it was. Unfortunately with PHP, adding those lines of whitespace to the end of the file caused this "Cannot modify header information - headers already sent" error message. (So, if I want to get TextMate to scroll more, I'm going to have to do something else, like put my blank lines before the closing tag.)

Also, in my case I specifically started getting this PHP error message when I added CakePHP Auth module handling to my application. Part of the CakePHP Auth component requires some "redirect" function calls, and I know from previous experience that you can't tell a page to redirect once you've sent anything to the browser. (Therefore, it's safe to say that you may never see this PHP error message unless you add redirect functionality to your application.)

So again, if you're editing a CakePHP application and you get this "Cannot modify header information" error message, take a look at your CakePHP model and CakePHP controller classes, and look for whitespace after the closing "?>" tag in your PHP file.