Thursday, 17 March 2011

PHP: Cannot send session cookie - headers already sent by .. on line 1

Just a quickie which may save you a bit of time - certainly buggered me up for a few minutes.

If you're doing something in PHP which requires starting a session, writing a cookie, etc and you get the dreaded "headers already sent" error, e.g: "Cannot send session cookie - headers already sent by xxx.php;1" - and the error line indicated after the semi colon is the first line, what do you do?

It's normally because you've got content above the header output - which means it can't be sent, but if it's line 1 at fault, theres normally no content.

Easy - at least in my case - check your file is saved on the server in UTF8 format - NOT ANSI. I was using Beyond Compare to upload/edit a file and it had changed the format without telling me - quick tweak - and everyone is happy again!