Question

I have a php script which is working perfectly on my localhost server.

When I moved everything from the localhost to the web server my json_decode is not working.

I have tried json_encode and still nothing.

what could be a problem for such behavior?

my code:

$productsArr = json_encode($_GET['object']);

$_GET['object'] is validated JSON.

My last option could be magic_quotes but I don't know if I can change PHP.ini file using a cPanel which is my only access to the server.

I would appreciate any ideas.

EDIT:

this is part of my url:

Request URL:http://something.com/download.php?object=[{%22code%22:%222F-58S%22},{%22code%22:%22HT-45H%22},{%22code%22:%2244-3%22},{%22code%22:%22898-OPv%22}]&checkbox=

I'm using this headers if this is even important:

header('Content-Description: File Transfer'); 
header("Content-type: application/ms-word");
header("Content-Disposition: attachment;Filename=$name_of_file.doc");
Was it helpful?

Solution

I had to do this to make json_decode work. Maybe there is a better scheme for this.

$j = $_POST["json"];
$j = str_replace("\\\\\"", "###dq###", $j);
$j = str_replace("\\", "", $j);
$j = str_replace("###dq###", "\\\"", $j);

or, in short:

$j = stripslashes($j);

OTHER TIPS

2 hints:
1-echo $_GET['object'] ,If see \ in the text use stripcslashes() and then json_decode().
2-If see stdclass error use this code:

$productsArr = json_decode($_GET['object'],true);

If magic Quotes are your problem, You could put this code at the top of your script entry points

function undoMagicQuotes($array, $topLevel=true) {
        $newArray = array();
        foreach($array as $key => $value) {
            if (!$topLevel) {
                $key = stripslashes($key);
            }
            if (is_array($value)) {
                $newArray[$key] = undoMagicQuotes($value, false);
            }
            else {
                $newArray[$key] = stripslashes($value);
            }
        }
        return $newArray;
    }
    $_GET = undoMagicQuotes($_GET);
    $_POST = undoMagicQuotes($_POST);
    $_COOKIE = undoMagicQuotes($_COOKIE);
    $_REQUEST = undoMagicQuotes($_REQUEST);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top