1

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);
});
fefe
  • 8,755
  • 27
  • 104
  • 180
  • it will only show last one because it will replace all and equal to last one please change index aur make ids[] as an array – Agha Umair Ahmed Jan 29 '14 at 11:50
  • What is the problem? This behaviour is expected. Please clarify what you want to do. And show us your jquery code. – Happy Jan 29 '14 at 11:50
  • Is the character encoding the same on both machines? Also, is this output printing on SO correctly? – krowe Jan 29 '14 at 11:55
  • what you mean my meta header? – fefe Jan 29 '14 at 11:58
  • Seems your AJAX is sending a JSON encoded string while your PHP is expecting an unencoded GET variable? – ToBe Jan 29 '14 at 12:48
  • okay but how is possible that this works on windows xampp and under ubuntu server don't. On xampp tho model is getting loaded and query executed – fefe Jan 29 '14 at 12:50
  • Can you verify that all data is using UTF-8, you php.ini specifies UTF-8 and your webbrowser shows UTF-8 as ists received header for your page? – ToBe Jan 31 '14 at 11:59
  • on both of servers the default_charset is uncommented and Content-Type text/html; charset=utf-8 – fefe Jan 31 '14 at 12:30
  • this one makes to get crazy! I don't understand why doesn'twork on ubuntu – fefe Jan 31 '14 at 12:53

1 Answers1

2

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.

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • Thank you for your feedback I will check it Monday. I hope this is the workaround – fefe Jan 31 '14 at 18:07
  • my phpinfo on live server shows that I'm on IPv6 I Have been disabling IPv6 support but phpinfo shows the same – fefe Feb 03 '14 at 12:11
  • @fefe: and what about suhosin? is that being used? and is it working using `http_get_request_body`, instead of using the flawed `$_POST` array? – Elias Van Ootegem Feb 03 '14 at 12:23
  • okay I just made an upgrade to php5.4, suhosin is not listed in php info but the problem still persist. Because of the complexity of my jquery ajax I would like to avoid to refactor – fefe Feb 03 '14 at 12:31
  • @fefe: on suhosin, you [may want to look at this](http://stackoverflow.com/questions/3383916/how-to-check-whether-suhosin-is-installed), it needn't always show up in `phpinfo`. The whole idea of this extension is security, so it may be there, but hidden from the `phpinfo` – Elias Van Ootegem Feb 03 '14 at 12:40
  • than I added [suhosin] suhosin.simulation = On to my php.ini as I understand this one should make the fix if suhosin extension is activated – fefe Feb 03 '14 at 13:25
  • @fefe: Not quite with you there, but have you been able to fix the issue? – Elias Van Ootegem Feb 03 '14 at 14:15
  • yes thank you for your feedback. After all having configured the Server upgrading packages the problem is solvedand the bug as you suggested was suhosin. Thanks again – fefe Feb 03 '14 at 14:38