BOM being added to any return or die response

1.1k views Asked by At

I'm using jQuery to retrieve a json response from an endpoint

die(json_encode(array('success' => 3, 'message' => 'You must use at least 1 credit or more.')));

Whenever I check the JSON response received in chrome developer tools I'm getting a red dot showing \ufeff is being added before the json response. I've encoded the PHP file with UTF-8 in Notepad++ however it still adds the BOM character infront of any response. If I return anything or change the die it will still show the BOM character in the response.

I've tried the same file on my localhost and it works absolutely fine however on the server it adds the character.

I'm at a loss as to what's causing the issue, any help would be greatly appreciated.

2

There are 2 answers

0
king_nak On BEST ANSWER

This is an 13 year old issue

There are workarounds (removing BOM from all PHP files, ob_clean at script start), but the real solution is to have a PHP compiled with --enable-zend-multibyte or --enable-mbstring, or wait until it is fixed by the PHP team.

As you sometimes have no control over the PHP version and compilation flags on hosted environments, I prefer removing BOMs from all PHP files, to prevent this kind of issues. This will work on any server.

Your solution is to fix the output with JS. But for other usages, e.g. generating an image or other binary data via PHP, or sending headers, you cannot solve this way.

0
Paradigm On

It seems it was specifically an issue with this server configuration as it works on other servers. For the meantime I've filtered the response to remove any BOM chracters using javascript before parsing the JSON response.