3

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");
user123_456
  • 5,635
  • 26
  • 84
  • 140
  • 2
    Try echoing $_GET['object'] and make sure it is what you think it is. Also should that code be json_decode() rather than json_encode() – Anigel Jul 29 '13 at 15:32
  • @Anigel I tried but is not echoing anything..weird?? anyway tried with only $_GET and still nothing...I can see that data is actually passed in the network window – user123_456 Jul 29 '13 at 15:33
  • In that case you have something wrong with your request. What URL are you requesting? You do have something after the filename don't you? eg http://blah.com/index.php?object=XXXXXXXX – Anigel Jul 29 '13 at 15:34
  • 2
    If `magic_quotes_gpc` is on and you cannot disable it using `php_flag magic_quotes_gpc off` in `.htaccess`, you could try `stripslashes()`. – PleaseStand Jul 29 '13 at 15:35
  • @PleaseStand if $_GET is empty then magic_quotes is not the problem – Anigel Jul 29 '13 at 15:36
  • What version of PHP is on the server... – Orangepill Jul 29 '13 at 15:38
  • if get is empty, then you need to investigate why the query string is being dropped before PHP is getting invoked. Note that sending large strings in query parameters is generally a bad idea. browser have length limits and may silently truncate any over-long strings. This will utterly kill a json string – Marc B Jul 29 '13 at 15:40
  • @Orangepill version is 5.3.17 – user123_456 Jul 29 '13 at 15:44
  • I have add my url in the question – user123_456 Jul 29 '13 at 15:49
  • Regarding the "length limits" Marc B is referring to, if the Suhosin-Extension is installed, that could cause a query string argument to be dropped. http://stackoverflow.com/questions/8485908/long-query-string-variable-missing – PleaseStand Jul 29 '13 at 15:52
  • @PleaseStand how can I check which version I have installed and if I have that version that you have put link on it – user123_456 Jul 29 '13 at 16:02
  • http://stackoverflow.com/questions/3383916/how-to-check-whether-suhosin-is-installed – PleaseStand Jul 29 '13 at 16:07
  • @PleaseStand it's not installed – user123_456 Jul 29 '13 at 16:12
  • this is when I do echo `[{\"code\":\"898-OPv\"}]` – user123_456 Jul 29 '13 at 16:16

3 Answers3

7

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);
Mundi
  • 79,884
  • 17
  • 117
  • 140
0

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);
ops
  • 2,023
  • 18
  • 21
  • 1 - `stripslashes` is the more appropriate function to use here. 2 - supposedly the code already works on one of the servers, so that probably isn't it. – PleaseStand Jul 29 '13 at 15:45
  • Please paste output line: `code`echo $_GET['object'];`code` here ;) – ops Jul 29 '13 at 15:57
  • You try this code: `code`print_r(json_decode(stripcslashes($_GET['object']),true));`code` I tested in localhost and get correct output. ;) – ops Jul 29 '13 at 16:23
-1

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);
Amanuel Nega
  • 1,899
  • 1
  • 24
  • 42