CakePHP and “Headers already sent… on session.php”

April 25, 2007 § 10 Comments

O boy… I’m in trouble now…

CakePHP project is probably the easiest project I’ve ever done in my life. Until I have to debug it…

Why all the sudden I got “Headers already sent… on session.php”?

Folks at Google Group had lots of talk about it. Most suggested that I have strange character or spaces before or after <?php ?> tag.

Nada… No such problem here in my code, double checked it three times.

Man, not even Google Group can help me this time.

Anyone of you kind souls out there willing to help this poor lad?


Ahsan comments make me to double check my cake’s php files. It turns out that i have a SINGLE white space on my /app/webroot/index.php. Man… a single whitespace… This is why I shouldn’t do initial development on Windoze machine.

Thanks Ahsan! 


§ 10 Responses to CakePHP and “Headers already sent… on session.php”

  • Ahsan says:

    Hello didip,

    make sure that there are no whitespaces before the first tag in all your controllers, default.thtml (thats the layout file), and possibly also in your model and view files. So, if you have a file like this:

    the should be the last thing in your file.

    Hope, I was useful.

  • Kris says:

    There are many factors to errer “header already sent” but most common one is the whitespace. I’ve just fixed mine so just like to share how i went through incase someone come accross one like mine.

    First, before writing cake, i thought this was ok..

    Since in my sense, the whitespace b/w “}” and “?>” is still in PHP server-side tag and should not outputing anything,
    but seems like cake do not except it.. it has to be:

    Notice that now the } and ?> is now on the same line. So I went through and fix this in all my model and redirect/session is now back to work.

    For me only deleting the white space b/w } and ?> sometime still won’t work. I have to **delete and retype the whole thing**.

    Don’t know why but it must be something to do with the encoding.

    Hope that was helpful.


  • Guiguch says:

    In case your file is encoded in UTF-8 make sure that the BOM part (Byte Order Mark) is not present.

    I got to remove it to make my controller working

  • Mark Lynch says:

    Thanks for the tip – saved me some nice time digging through code.


  • Daniel says:

    Can’t believe a single space would cause such a disaster.. Mine was not at the index.php but at a controller file. I would advice anyone to look VERY closely at the opening <?php tag and avoid entering any spaces after that. Thank you!!! this was a huge time saver.


  • Chris Barnes says:

    Yep! had this same problem. It is almost certainly a whitespace issue. If you use Coda as your text editor there is a plugin that will remove whitespace from your document.

    Note that this header error will also cause the SWXExplorer to give you a null object reference if you are using AmfPhp.

    I hope this saves someone else time.

  • Gabriel says:

    You have just save my day. Better yet, my week.

  • Mohammed says:

    Thanks for the “white space” tip, it drove me craaaazy

  • Laurie says:

    I spent ages trying to figure this out I was adamant I did not have any whitespace… I was correct. I found another cause of this error. It only occured when I used setFlash to output a message. I was leaving off the “true” at the end of setFlash.


    $this->Session->setFlash(__(‘Your message’));


    $this->Session->setFlash(__(‘Your message’, true));

  • mauro says:

    I have the same problem! I’m develop on windows with the notepad++ and the problem is not exactly a whitespace, my problem was I saved the document with utf-8 encoding :S. When I restore the enconding to the default ANSI, all works again. The bad thing is the I was saving the document in utf-8 because cakephp don’t shown the strings with especial characters like: áíúñ.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

What’s this?

You are currently reading CakePHP and “Headers already sent… on session.php” at RAPD.


%d bloggers like this: