简体   繁体   中英

Problems with encoded array in php

I have problem to parse a get variable which contains an array and send it with jquery ajax.

the array what I send can look as it follows

ids%5B%5D   1403172219
ids%5B%5D   1530542001
ids%5B%5D   1582588379

but when I try to dump( $_GET['ids] ) I get null I dumb the hole $_GET than looks like

ids%5B%5D] => 1530542001 

JQuery

function update_category(selected) {
    console.log(cids);
    $.ajax({
        url: '/admin/',
        type: 'GET',
        dataType: "application/JSON",
        data: {
            ids: cids,
            s_category: selected,
            controller: 'products',
            traditional: true,
            action: 'update_category'
        },
        success: function(data) {
            addAlert('alert-'+data, data);
        },
        error: function(data) {
            addAlert('alert-'+data.responseText, data.responseText);
        }
    });

controller

  protected function update_category() {
        print_r($_GET);
        $cat_ids = $_GET['ids'];
        $category = $_GET['s_category'];       
        if(!empty($cat_ids) && !empty($category)){
            $this->model->updateCategory($cat_ids, $category);
        } else {
            echo 'Fähler in der Daten übergabe!';
            exit();
        }              
    }
}

on localhost I use PHP 5.4 and I do not face any problems on live Server ubuntu PHP 5.3.10 the error persist

the correct output on localhost

Array ( 
[ids] => Array (
    [0] => B6Q09EA#ABD 
    [1] => H4P04EA#ABD 
    ) 
[s_category] => 4
[controller] => products 
[traditional] => true 
[action] => update_category 
) 

ids are builded like

cids = [];
jQuery.each(sData, function(key, value) {
       cids.push(value.value);
});

Your live server will probably be an IPv6 enabled host. That means that [ and ] (or %5B and %5D ) are reserved characters. encodeURI , which is applied to your stringified request string, doesn't, or rather cannot encode these chars properly, just as & and = aren't encoded.
The issue here is the PHP version (or maybe suhosin) you have installed on your server, I think. I make this assumption based on this bug report , where one response stated the following:

I just updated from PHP 5.3 (with suhosin 0.9.33) to PHP 5.5 (without suhosin) and this fixed the issue. I guess some security stuff within suhosin might be the cause for the spinning icon issue I had.

I could switch back to PHP 5.3 and remove the suhosin module to further investigate this, but I think the public interest might not be too high as I seem to be the only reported case with this problem. :-)

verify if your server has suhosin installed, and check your phpinfo() on both servers, that should fix it.
Alternatively, you could choose to not use jQuery for this, and write your own XMLHttpRequest , and add a encodeURI(stringifiedObject).replace(/%5B/g, '[').replace(/%5D/g,']'); before sending the data.

Some more details on the IPv6 thing: on MDN

A temporary workaround/fix might be to fix the request server-side, if you're not willing to ditch jQ:

$post = http_get_request_body();//requires http extension
$post = str_replace(
    array('%5B', %5D'),
    array('[',']'),
    $post;
);

Then using parse_str , you can construct the $_POST array:

parse_str($post, $_POST);

That should give you the array you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM