问题描述:

$a = $_REQUEST['label'];

Now how can i recognize whether the value stored in variable $a was from $_GET or from $_POST? I wanted to redirect user if it was collected from $_GET. Is there any way to check ? PHP is little tough. Just something like this:

$var = recognize($_REQUEST['label']);

if($var == 'GET') { } else { }

网友答案:

Once a variable has been assigned, you won't be able to tell where it came from (usually).

Consider doing something like this, because if you use $_REQUEST it could even come from $_COOKIE!

if (isset($_GET['label'])) {
  // do redirect
} elseif (isset($_POST['label'])) {
  // do something else
}

Or, if you're passing that variable deep down where you can't tell where it originally came from:

class RequestParameter
{
    private $name;
    private $value;
    private $source;

    public function __construct($name)
    {
        $this->name = $name;
        if (isset($_POST[$name])) {
            $this->value = $_POST[$name];
            $this->source = INPUT_POST;
        } elseif (isset($_GET[$name])) {
            $this->value = $_GET[$name];
            $this->source = INPUT_GET;
        }
    }

    public function isFromGet()
    {
        return $this->source === INPUT_GET;
    }

    public function getValue()
    {
        return $this->value;
    }
}

$a = new RequestParameter('label');

if ($a->isFromGet()) {
    // do redircet
}

But I would suggest structuring your code in a way that this is not necessary. One way is to check whether a POST was made:

$_SERVER['REQUEST_METHOD'] === 'POST'
网友答案:

Check for if($_GET['label']) { then redirect using header location; }

网友答案:

Better use $_SERVER['REQUEST_METHOD']:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // …
}

more details see documentation PHP

网友答案:

try this

if(isset($_REQUEST['label'])){
//redirect
}
相关阅读:
Top