php 致命错误:无法重新分配自动全局变量 _POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22583386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:07:27  来源:igfitidea点击:

Fatal error: Cannot re-assign auto-global variable _POST

phpwordpressthemes

提问by user3450716

I can't get access to my WP (version3.4.2) admin. It says as mentioned above

我无法访问我的 WP(版本 3.4.2)管理员。它说如上所述

Fatal error: Cannot re-assign auto-global variable _POST in /home/xxx/public_html/wp-content/themes/rtthemes16/rt-framework/classes/admin.php on line 540.

致命错误:无法在第 540 行的 /home/xxx/public_html/wp-content/themes/rtthemes16/rt-framework/classes/admin.php 中重新分配自动全局变量 _POST。

The line 540 is :

第 540 行是:

function rt_check_sidebar_array($_POST){

    if(is_array($_POST)){

        $start_unset_count = 0;

        foreach($_POST as $key => $value){
            if(stristr($key, '_sidebar_name') == TRUE && $value=="") {                  
                unset($_POST[$key]);
                $start_unset_count = 1;
            }

            if($start_unset_count>0){
                unset($_POST[$key]);
                $start_unset_count++;
            }

            if($start_unset_count==6){
                $start_unset_count = 0;
            }               
        }
    }


    $newPost == $newPost ? $newPost : $_POST;       
    return $_POST;
}

Any insights? Thanks :)

任何见解?谢谢 :)

回答by Abhik Chakraborty

Since PHP 5.4, you cannot use a superglobal as the parameter to a function

自 PHP 5.4 起,您不能使用超全局变量作为函数的参数

$_POST is globally accessible. So you don't have to pass to your function.

$_POST 是全局可访问的。所以你不必传递给你的函数。

http://php.net/manual/en/language.variables.superglobals.php#112184

http://php.net/manual/en/language.variables.superglobals.php#112184

This is how your function should look like

这就是你的函数的样子

function rt_check_sidebar_array(){

    if(is_array($_POST)){

        $start_unset_count = 0;

        foreach($_POST as $key => $value){
            if(stristr($key, '_sidebar_name') == TRUE && $value=="") {                  
                unset($_POST[$key]);
                $start_unset_count = 1;
            }

            if($start_unset_count>0){
                unset($_POST[$key]);
                $start_unset_count++;
            }

            if($start_unset_count==6){
                $start_unset_count = 0;
            }               
        }
    }


    $newPost == $newPost ? $newPost : $_POST;       
    return $_POST;
}

回答by Sebastian Lucaci

@user3450716, the only thing you need to do, as Abhik Chakraborty said, is to delete the $_POSTfrom your function rt_check_sidebarparameters and leave the function with no parameters, like this:

@ user3450716,正如 Abhik Chakraborty 所说,您唯一需要做的就是$_POST从您的函数rt_check_sidebar参数中删除 ,并使该函数不带任何参数,如下所示:

your line 540:

您的第 540 行:

function rt_check_sidebar_array($_POST){

change it to:

将其更改为:

function rt_check_sidebar_array(){

回答by Nick Kravchenko

@user3450716. You can't change superglobal variables too, so you can't use unset($_POST[$key])

@用户3450716。你也不能改变超全局变量,所以你不能使用unset($_POST[$key])

function rt_check_sidebar_array(){
    $post = $_POST;
    if(is_array($post)){

        $start_unset_count = 0;

        foreach( $post as $key => $value ){
            if( stristr( $key, '_sidebar_name' ) == TRUE && $value == "" ) {                  
                unset( $post[ $key ] );
                $start_unset_count = 1;
            }

            if( $start_unset_count > 0 ){
                unset( $post[ $key ] );
                $start_unset_count++;
            }

            if( $start_unset_count == 6 ){
                $start_unset_count = 0;
            }               
        }
    }

    // idk why you wrote this,
    // because $newPost variable isn't used in the code above and below
    $newPost == $newPost ? $newPost : $post;

    return $post;
}