Cannot Send Session Cookie Even With no HTML \ White space – The Fix
Sep 09
Sometimes standard things can cause the most trouble when coding. Today I was working on a CMS for a client, the project was simple and I set about 4 hours aside for it, leaving plenty of time to work on the other projects that I have needing to be done, but then as sods law tends to be I got hit by a PHP Error. The error?
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by…
I’ve had this plenty of times, and the first thing you should always check is that your
<?php session_start(); ?>
is above all HTML. This means at the very top of your text document, making sure you don’t even have a blank line. If you don’t have any space or any code it can be somewhat confusing, and that’s what happened to me today. I had everything looking exactly as it should and the code looked like it should work perfectly, but never the less I was still getting the session_start error. 2 Hours of looking over my script and code came to a end when I finally worked it out, I used Dreamweaver CS4 beta to create some of the code, and without me knowing Dreamweaver had added a unicode signature also known as BOM. This BOM is not visible in source code view, nor in notepad in fact the majority of PHP editors won’t show you this character. To turn it off it quite simple. (presuming Dreamweaver CS2\3\4)
In the document that is having a session_start() error go to Modify page properties (or press Ctl & J). In the window that opens go to the title/encoding tab and untick the ‘Include Unicode Signature’ then click apply. You should now not get the session control error.