Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_decode fails on godaddy hosting

I use json_decode 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);
    exit;
}

...  //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 < or > characters in the values of JSON object properties.

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

$.ajax({
    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.";
        alert(msg);
    }
})

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

{
    "scenarioId":"1",
    "scenarioName":"MOH",
    "startTime":1355496349,
    "log":[
        {
         "role":"Leader",
         "task":"MOH",
         "response":"start",
         "time":1355496349
        },
        {
         "role":"Head",
         "task":"<span class=\"bold\">Assign Role</span>",
         "response":"done",
         "time":1355496351
        }
    ]
}

If the last element in the array (with task Assign Role) is removed, then everything is processed successfully. If I replace < and >> 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 JSON.stringify 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?

like image 638
Aleks G Avatar asked Dec 14 '12 15:12

Aleks G


1 Answers

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.

like image 175
Aleks G Avatar answered Oct 31 '22 13:10

Aleks G