现在,我如何识别存储在变量$a的值是来自$_GET还是来自$_POST ? 如果用户是从$_GET收集的,我想重定向用户。 有没有办法检查? PHP有点难度。 就像这样:
$var = recognize($_REQUEST['label']); if($var == 'GET') { } else { } $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 { }最满意答案
一旦赋值变量,您将无法分辨它(通常)的来源。
考虑做这样的事情,因为如果你使用$_REQUEST它甚至可以来自$_COOKIE !
if (isset($_GET['label'])) { // do redirect } elseif (isset($_POST['label'])) { // do something else }或者,如果您将该变量传递到深层,您无法分辨它最初的来源:
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 }但我建议以不必要的方式构建代码。 一种方法是检查是否进行了POST:
$_SERVER['REQUEST_METHOD'] === 'POST'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'识别$ _REQUEST(Recognize $_REQUEST) $a = $_REQUEST['label'];现在,我如何识别存储在变量$a的值是来自$_GET还是来自$_POST ? 如果用户是从$_GET收集的,我想重定向用户。 有没有办法检查? PHP有点难度。 就像这样:
$var = recognize($_REQUEST['label']); if($var == 'GET') { } else { } $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 { }最满意答案
一旦赋值变量,您将无法分辨它(通常)的来源。
考虑做这样的事情,因为如果你使用$_REQUEST它甚至可以来自$_COOKIE !
if (isset($_GET['label'])) { // do redirect } elseif (isset($_POST['label'])) { // do something else }或者,如果您将该变量传递到深层,您无法分辨它最初的来源:
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 }但我建议以不必要的方式构建代码。 一种方法是检查是否进行了POST:
$_SERVER['REQUEST_METHOD'] === 'POST'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'
发布评论