Headers Already Sent !

Many people working with PHP would have encountered this very annoying “headers already sent” error, when working with Cookies or Sessions. This error appears, when some HTML output is sent, before the session/cookie handling begins.

 

Most of the Time, an Extra character before the PHP Tag is the reason. Even an Extra space before the PHP starting Tag, will Trigger is annoying error. Now, It may appear that even if there are no additional characters before the Tag, the error still appears ! Sounds, Fishy right ?

 

This may happen due to the Text editor that is used to Edit the php file. Probably, the Text editor inserted some additional character, which generated this error. Ah, that last one was the one I had to face.

I finally figured out the the Encoding of the file was the main reason. I had save the file as UTF-8 encodiung in the editor. The editor added extra information (BOM) at the beginning of the file, to identify the encoding as UTF-8.

 

There was noway, that I could the file as plain Text, I worked with Multi-Lingual Text. Everything would become appear as question marks if I had to force plain text.  Luckily, Editors like Notepad++ provide the option to save the file in UTF-9 without BOM. Presto ! problem solved :-). So, if you are working with multilingual PHP files, always save in UTF-8 without BOM.

 

[Another instant and dirty work-around is to save the entire body portion of the PHP as a separate PHP file with UTF encoding, and include it in the main file 🙂 ]

 

So, if any of you guys any such issues try the above, probably it wold clear the issue.

Leave a Reply

Your email address will not be published. Required fields are marked *