Reputation: 25392
I am setting up a game server on my dedicated server using PHP and I have run into this problem. I have a script that I run to get the game state as JSON.
To save bandwidth and processing time, I decided that on the update script, I want to directly run this same script after I update the server, so I just include it:
include "{$_SERVER["DOCUMENT_ROOT"]}/game/get/get_session_info.php";
This does work, but the problem is that this include statement inserts a single invisible character before the output of that script that makes it impossible to parse as JSON.
I know it is from the include, because for every include I put in the script, one of these invisible characters is inserted.
If I run that script directly from its URL, the length is 8014; if I run it from my update script, the length is 8015.
How can I prevent PHP from inserting that extra character, or run the script and return its results differently so that this does not happen?
get_session_info.php
is echo trim(json_encode($game));
Here is the start of the string from the update script:
{"state":2,"players":4,"loc1":"15,25,35",
And in HEX:
feff7b227374617465223a322c22706c6179657273223a342c226c6f6331223a2231352c32352c3335222c
And the same string from get_session_info.php
:
{"state":2,"players":4,"loc1":"15,25,35",
And in HEX:
7b227374617465223a322c22706c6179657273223a342c226c6f6331223a2231352c32352c3335222c
It appears to be inserting feff
for each include (except the one that includes my mysqli_connect, actually...).
Upvotes: 0
Views: 159
Reputation: 1427
It's not the include that generates the invisible character, but most probably the file you are including. Check for whitespaces outside PHP scope in that file.
Upvotes: 1