Aleks G Aleks G - 1 year ago 118
PHP Question

json_decode fails on godaddy hosting

I use

in my PHP to parse data from POST (the POST itself is executed via ajax). My code looks like this:

$data = json_decode($_POST['logdata'], true);
if(!$data) {
$ecodes = array(
JSON_ERROR_NONE => "No error has occurred",
JSON_ERROR_DEPTH => "The maximum stack depth has been exceeded",
JSON_ERROR_STATE_MISMATCH => "Invalid or malformed JSON",
JSON_ERROR_CTRL_CHAR => "Control character error, possibly incorrectly encoded",
JSON_ERROR_SYNTAX => "Syntax error",
JSON_ERROR_UTF8 => "Malformed UTF-8 characters, possibly incorrectly encoded"
$err = json_last_error();
$result = array("error" => 1, "error_msg" => "Invalid log data: " . $ecodes[json_last_error()]);
echo json_encode($result);

... //use $data array

This code works fine. When executed on my local linux machine or my mac, the passed data is decoded correctly and everything works fine. However when the same code is executed on godaddy shared hosting, the decoding fails with Syntax error. Having spent a lot of time trying to narrow down the problem, I found out that the problem occurs when I have
characters in the values of JSON object properties.

The uploading part (ajax) in javascript looks like this:

url: '/emaillog.php',
type: 'POST',
dataType: 'json',
data: { logdata: JSON.stringify(this.logData[this.scenarioLogId]) },
cache: false,
error: function(jqXHR, textStatus, errorThrown) {
alert("Error occurred during upload: " + textStatus);
success: function(data, textStatus, jqXHR) {
var msg = data.error
? "An error occurred processing action log: " + data.error_msg
: "Action log processed successfully.";

As you can see, the value of the POST parameter is created using
and is correct on inspection (both client and server side). If it matters, here's an example value of the submitted POST parameters:

"task":"<span class=\"bold\">Assign Role</span>",

If the last element in the array (with task Assign Role) is removed, then everything is processed successfully. If I replace
> characters with spaces, again, everything is fine.

Two questions arise from this:

  1. If syntax error on such a JSON string is the correct behaviour, then why do I not see the error in the other two environments? Also, why is
    then producing an incorrect string?

  2. If syntax error on such a JSON string is an incorrect behaviour, then something must be wrong with godaddy's PHP implementation or configuration.

Note that, while I explicitly asked about godaddy, it's only relevant as insofar it's the only provider where the code produces an error. Anybody can shed some light on this situation?

Answer Source

For whatever reason, on godaddy hosting only (maybe in some other places too, but not in other places I tested), when I was submitting the form, the server was escaping quotes - and that's what was causing the issue. The solution to the problem was quite simple: instead of doing

$data = json_decode($_POST['logdata'], true);

I did

$data = json_decode(stripslashes($_POST['logdata']), true);

and everything worked perfectly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download