简体   繁体   English

PHP中编码数组的问题

[英]Problems with encoded array in php

I have problem to parse a get variable which contains an array and send it with jquery ajax. 我在解析包含数组的get变量并使用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 但是当我尝试转储( $_GET['ids] )时,我得到的是空值,我将$_GET孔比看起来小

ids%5B%5D] => 1530542001 

JQuery 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 在本地主机上,我使用PHP 5.4,在Live Server ubuntu PHP 5.3.10上我没有遇到任何问题,错误仍然存​​在

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 ID的建立像

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

Your live server will probably be an IPv6 enabled host. 您的实时服务器可能是启用IPv6的主机。 That means that [ and ] (or %5B and %5D ) are reserved characters. 这意味着[] (或%5B%5D )是保留字符。 encodeURI , which is applied to your stringified request string, doesn't, or rather cannot encode these chars properly, just as & and = aren't encoded. 应用于您的字符串化请求字符串的encodeURI不会,或者不能正确编码这些字符,就像&=未被编码一样。
The issue here is the PHP version (or maybe suhosin) you have installed on your server, I think. 我认为这里的问题是您已在服务器上安装的PHP版本(或suhosin)。 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. 我刚刚从PHP 5.3(带有suhosin 0.9.33)更新到PHP 5.5(没有suhosin),这解决了这个问题。 I guess some security stuff within suhosin might be the cause for the spinning icon issue I had. 我猜想suhosin中的一些安全问题可能是导致我出现旋转图标问题的原因。

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. 我可以切换回PHP 5.3并删除suhosin模块以进一步研究此问题,但我认为公众利益可能不会太高,因为我似乎是唯一报告此问题的案例。 :-) :-)

verify if your server has suhosin installed, and check your phpinfo() on both servers, that should fix it. 验证您的服务器是否已安装suhosin,并在两台服务器上检查phpinfo() ,这应该可以对其进行修复。
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,']'); 或者,您可以选择为此使用jQuery,并编写自己的XMLHttpRequest ,并添加一个encodeURI(stringifiedObject).replace(/%5B/g, '[').replace(/%5D/g,']'); before sending the data. 在发送数据之前。

Some more details on the IPv6 thing: on MDN 有关IPv6的更多详细信息: 关于MDN

A temporary workaround/fix might be to fix the request server-side, if you're not willing to ditch jQ: 如果您不愿意放弃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数组:

parse_str($post, $_POST);

That should give you the array you need. 那应该为您提供所需的阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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