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

24,242

Solution 1

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

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

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;
}

Solution 2

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

your line 540:

function rt_check_sidebar_array($_POST){

change it to:

function rt_check_sidebar_array(){

Solution 3

@user3450716. You can't change superglobal variables too, so you can't use 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;
}
Share:
24,242
user3450716
Author by

user3450716

Updated on July 20, 2020

Comments

  • user3450716
    user3450716 almost 4 years

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

    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.

    The line 540 is :

    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 :)

  • user3450716
    user3450716 about 10 years
    Can you be more precise (bc the link suggests i rename the variable $)
  • Abhik Chakraborty
    Abhik Chakraborty about 10 years
    if you rename your function param name then you can access $_POST data via that param name, the best thing would be to have no param at all in function rt_check_sidebar_array($_POST){ and should be as function rt_check_sidebar_array(){ since you can access $_POST inside the function directly for any operation and this is true for any super globals like $_FILES, $_GET etc.
  • user3450716
    user3450716 about 10 years
    still the same line; 540
  • user3450716
    user3450716 about 10 years
    Check sidebar array # 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; }
  • Abhik Chakraborty
    Abhik Chakraborty about 10 years
    no u have not removed the $_POST in function param function rt_check_sidebar_array($_POST) it should be just function rt_check_sidebar_array() and when u call this function just call it without any param.
  • user3450716
    user3450716 about 10 years
    theres the new one # # Check sidebar array # function rt_check_sidebar_array($_POST){ if(is_array()){ $start_unset_count = 0; foreach( as $key => $value){ if(stristr($key, '_sidebar_name') == TRUE && $value=="") { unset([$key]); $start_unset_count = 1; } if($start_unset_count>0){ unset([$key]); $start_unset_count++; } if($start_unset_count==6){ $start_unset_count = 0; } } } $newPost == $newPost ? $newPost : ; return ; }
  • Abhik Chakraborty
    Abhik Chakraborty about 10 years
    in the new one also u have function rt_check_sidebar_array($_POST).. please remove $_POST from the function parameter
  • user3450716
    user3450716 about 10 years
    Check sidebar array # function rt_check_sidebar_array(){ if(is_array()){ $start_unset_count = 0; foreach( as $key => $value){ if(stristr($key, '_sidebar_name') == TRUE && $value=="") { unset([$key]); $start_unset_count = 1; } if($start_unset_count>0){ unset([$key]); $start_unset_count++; } if($start_unset_count==6){ $start_unset_count = 0; } } } $newPost == $newPost ? $newPost : ; return ; } #
  • Abhik Chakraborty
    Abhik Chakraborty about 10 years
    u are doing wrong again check my updated answer and I would suggest to learn more on PHP functions and arguments.
  • user3450716
    user3450716 about 10 years
    if u could send me the sentence i should use that would b very much appreciated.. im trying to fix things, every one is not as pro as u ..thanks
  • Abhik Chakraborty
    Abhik Chakraborty about 10 years
    sentence ?? did not get what u mean.
  • JayJay123
    JayJay123 over 7 years
    Worked like a charm in fixing the install of MyITCRM.