16

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?

4
  • I have tried to encode with JS and decode with my local PHP installation and it went okay (duh). I think you need to fire a ticket with GoDaddy.
    – ualinker
    Dec 14, 2012 at 16:15
  • @ualinker hello, here someone from the future. Did they respond to your ticket?
    – Braiam
    Aug 28, 2016 at 22:25
  • Linking for future visitors: Why json_decode doesn't work for me? Aug 29, 2016 at 5:46
  • @Braiam hey, are you guys have got flying cars already? I don't host with GoDaddy - the TS does.
    – ualinker
    Aug 30, 2016 at 15:05

1 Answer 1

14

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.

1
  • 7
    Though it can't really be confirmed now, your issue was probably caused by the very obsolete Magic Quotes feature being turned on. Way back when, it probably wasn't too uncommon for shared hosts to have this enabled. Aug 29, 2016 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.