Techblog Index

So your PHP headers are already sent, but you sent nothing?

So you are coding and your transfer your files to your production server via SVN or FTP and then you start getting some “Warning: Cannot modify header information – headers already sent” errors. Well, this is not good indeed.

This errors happens when you send a header command and has already outputted text to your output buffer (generally, the browser). Then you grep your code for misleft debugging prints, echos, print_rs and var_dumps but they are gone or commented.

Oh yeah, somehow some data were added during the transfer somewhere in your source files. Great! Now some cool action!

DON’T PANIC
As the 1st rule of code debugging, read the error message.

Warning: Cannot modify header information – headers already sent by (output started at /var/www/projects/…/app/models/Keyword.php:168) in /var/www/…/app/lib/AppController.class.php on line 421

It is all there. Notice the part where it says “output started at”. This tells you where to look for the added ghost data, the file and the line. Couldn’t be easier, could it? Just go to the line and trigger your home and end keys and the cursor will reveal the menace. Be ruthless! Just remove them and reload.

The second part tells you here your header command was triggered, again, file and line.

Some people (like people from Zend) tells users to never mind the php close tag ?> so you don’t get bothered by these errors. This is due PHP ignoring spaces within his enclosure tags but it prints the spaces out of them so if you don’t close the tag, there will be no tags out of them.

About this discussion, I choose keeping the close tag. As I said at the debate in a previous article: “Its not needed, ok. But it exists (and it is part of the code structure) so it shall be used.” and “If your FTP or IDE or ANYTHING puts unwanted data on your code, switch your application. Accidental white spaces. Accident happens when errors happens. A stable code/environment shall not have ‘accidents’”, but if you do, just read the error code and clean it up.

 
 

Reader's thoughts on "So your PHP headers are already sent, but you sent nothing?"

3
  1. Note that if you have a whitespace BEFORE your redirect , that also counts as a header ( HTML output )
    like:

    [whitespace_here]

  2. That’s 2 clvere by half and 2×2 clever 4 me. Thanks!

Leave a Reply