search

Home  >  Q&A  >  body text

Can classes in PHP judge the received parameters?

classInput.php
<?php
header("Content-Type:text/html; charset=UTF-8");
classInput{
    function post($key){
                $val=$_POST[$key];
                return $val;
    }
}
?>
save.php
<?php
header("Content-Type:text/html; charset=UTF-8");
include 'classInput.php';
$input=new Input();
$userName=$input->post('userName');
$msg=$input->post('msg');

Then I accessed save.php directly and reported a notice error. Can I judge the passed parameters in classInput.php?

世界只因有你世界只因有你2757 days ago391

reply all(4)I'll reply

  • 漂亮男人

    漂亮男人2017-05-16 13:06:59

    header("Content-Type:text/html; charset=UTF-8");
    class Input{
        function post($key){
                    if( isset($_POST[$key]))
                        $val=$_POST[$key];
                    else
                        $val=null;
                    return $val;
        }
    }

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-16 13:06:59

    <?php
    header("Content-Type:text/html; charset=UTF-8");
    class Input{
        function post($key){
            if(isset($_POST[$key])){
                    $val=$_POST[$key];
                    return $val;
            }
        }
    }
    ?>

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-16 13:06:59

    First, determine whether it is a post request. If it is a post request, then determine whether the value exists.

    reply
    0
  • 阿神

    阿神2017-05-16 13:06:59

    Made some modifications to your class

    <?php
    class Input{
        var $_Get='';
        var $_Post='';
        function __construct($data){
            print_r($data);
            $this->_Get  = $data['get'];
            $this->_Post = $data['post'];
        }
        function post($key){
            $data = $this->_Get;
            $val  = $data[$key];
            return $val;
        }
    }
    
    //然后调用的时候
    include 'classInput.php';
    $input=new Input(['get'=>$_GET]);
    $userName=$input->post('userName');
    echo $userName;
    //这样就好了把 
    

    reply
    0
  • Cancelreply