$a = $_REQUEST['label'];现在我如何识别存储在变量$a中的值是来自$_GET还是来自$_POST?我想要重定向用户,如果它是从$_GET收集的。有什么方法可以检查一下吗?PHP有点难。就像这样:
$var = recognize($_REQUEST['label']);
if($var == 'GET') { } else { }发布于 2013-02-23 15:07:52
一旦变量被赋值,你就不能知道它来自哪里(通常)。
考虑这样做,因为如果您使用$_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
}但我建议以一种不必要的方式来组织你的代码。一种方法是检查是否发布了帖子:
$_SERVER['REQUEST_METHOD'] === 'POST'发布于 2013-02-23 15:08:30
检查if($_GET['label']) { then redirect using header location; }
发布于 2013-02-23 15:52:19
最好使用$_SERVER‘’REQUEST_METHOD‘
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// …
}更多详细信息,请参阅文档PHP
https://stackoverflow.com/questions/15038057
复制相似问题