2

I'm experiencing a lot of issues with 5.4+ lately, one of them is redirects doesn't work, errors such as "headers already sent".

What I've noticed is that If I save file as "UTF-8 without BOM" issue go away (I guess there is some char I'm not really seeing).

How could I remedy this issue, so I don't have to convert every file to "UTF-8 without BOM"?

Thank you!

hakre
  • 193,403
  • 52
  • 435
  • 836
ProDraz
  • 1,283
  • 6
  • 22
  • 43

3 Answers3

2

A BOM (Byte Order Marker) consists of 3 bytes (0xEF 0xBB 0xBF) which indicate the endianness of the system on which it was generated (i.e. little-endian on Intel/x86 compatible processors, big-endian on pretty much anything else). It is not needed for UTF-8 and yes, you should not have it in your file.

The problem is that, since a BOM is at the start of a string (and thus of a file), it is inserted before any opening <?php you have and is therefore sent to the browser.

I'm not sure why PHP 5.4 does not ignore it and previous versions should, but you should simply not have it in your file if it is UTF-8.

Steven Don
  • 2,436
  • 15
  • 14
  • I'm really confused why all of the sudden after upgrade from PHP 5.3.x to 5.4.x is this an issue. – ProDraz Feb 10 '14 at 18:00
  • I've never seen earlier versions of PHP ignore BOMs (i.e., you always got the "headers already sent"). Anyway, the cure is to configure your editor(s) to never save with BOM. P.S. a BOM is 3 bytes, and if you put your editor (or browser) in a single byte mode (such as Latin-1), you'll see it displayed. – Phil Perry Feb 10 '14 at 18:04
  • 1
    Perhaps `output_buffering` was active before, but disabled when you upgraded to 5.4 … – CBroe Feb 10 '14 at 18:06
  • It's not a change as far as I can tell. There's nothing in the [upgrade notes](http://php.net/manual/en/migration54.changes.php) and there's people who've had this problem 4 years ago... – Steven Don Feb 10 '14 at 18:07
  • http://www.php.net/manual/en/outcontrol.configuration.php lists the default value as `0`, which equals to `off`. It might be that someone had it set to on (or a certain byte value) explicitly in your 5.3 installation before … – CBroe Feb 10 '14 at 18:13
0

https://bugs.php.net/bug.php?id=42312&edit=1 can give you some information and cross-references (see the comments), but it looks as though you simply need to work around it (though one of the commenters suggests a compilation option which might make life easier).

Brian Warshaw
  • 22,657
  • 9
  • 53
  • 72
-1

"headers already sent" usually appears when you try to do a header redirect

header("Location: some URL");

after you have already output some html.

To fix this, do all of your php checking and header redirects before calling get_header() (wordpress) or including header.php (CMS)

Kyle Tripp
  • 79
  • 6
  • http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Kyle Tripp Feb 10 '14 at 18:01
  • Yes, but I've figured out that headers are being already sent out, because of that white space in the file. Which I can't really see in notepad or other editors. – ProDraz Feb 10 '14 at 18:01