Unexpected PHP session_start() error

When you write PHP code with sessions, and without any apparent reason you got a message like

ERROR Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by…

and you are sure that the session_start() function is before any other code, or you don’t have made any other mistake, don’t start checking your code or make any changes at it.

Maybe it’s a problem with the file encoding. Using session_start() function, in a unicode encoded file mainly at MS Windows throws the above error, because many Windows programs using Unicode with BOM and add the bytes 0xEF, 0xBB, 0xBF at the start of any document. So, this sends the standard html header before the cookie and makes this the php statement invalid.

The solution is to change the file encoding to Unicode WITHOUT BOM. If you’re using Notepad++, just go the Encoding menu and make the appropriate selection, as shown in the image.