为什么这行会null
在我的实时服务器中返回?
filter_input(INPUT_SERVER, 'REQUEST_METHOD');
Run Code Online (Sandbox Code Playgroud)
实时服务器是php5.5.9
我错过了什么吗?
我以为它用于替换下面的全局方法?
$_SERVER['REQUEST_METHOD'];
Run Code Online (Sandbox Code Playgroud)
一些代码,
public function __construct()
{
// Construct other generic data.
$this->clientRequestMethod = filter_input(INPUT_GET, 'method'); // such as list, add, update, etc
$this->clientPostMethod = filter_input(INPUT_POST, 'method'); // such as update
$this->serverRequestMethod = filter_input(INPUT_SERVER, 'REQUEST_METHOD'); //such as get or post
}
public function processEntry()
{
// Determine the $_SERVER['REQUEST_METHOD'] whether it is post or get.
if ($this->serverRequestMethod === 'POST' && $this->clientPostMethod != null)
{
$this->processPost();
}
else if($this->serverRequestMethod === 'GET' && $this->clientRequestMethod != null)
{
$this->processRequest();
}
}
Run Code Online (Sandbox Code Playgroud)
Gre*_*gor 14
所以问题/错误是这样的:
filter_input()不工作INPUT_SERVER或INPUT_ENV当您使用FASTCGI
这个bug已经被人们所知多年了,我没有发现它被解决了.我找到了几个解决方法,但没有完整的解决方案,所以我将最好的解决方案填入了这个帮助函数,以实现项目范围的解决方案.为了提供某种程度的安全性并避免火车残骸,该函数会回退到filter_var(),其中filter_input()失败.它使用与本机filter_input()函数相同的格式,以便轻松集成到项目中,并且如果错误得到修复,将来很容易删除.
function filter_input_fix ($type, $variable_name, $filter = FILTER_DEFAULT, $options = NULL )
{
$checkTypes =[
INPUT_GET,
INPUT_POST,
INPUT_COOKIE
];
if ($options === NULL) {
// No idea if this should be here or not
// Maybe someone could let me know if this should be removed?
$options = FILTER_NULL_ON_FAILURE;
}
if (in_array($type, $checkTypes) || filter_has_var($type, $variable_name)) {
return filter_input($type, $variable_name, $filter, $options);
} else if ($type == INPUT_SERVER && isset($_SERVER[$variable_name])) {
return filter_var($_SERVER[$variable_name], $filter, $options);
} else if ($type == INPUT_ENV && isset($_ENV[$variable_name])) {
return filter_var($_ENV[$variable_name], $filter, $options);
} else {
return NULL;
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎是最好的解决方案.如果它包含可能导致问题的错误,请告诉我.
我有同样的问题,它在我的本地机器(OSX Mavericks,PHP版本5.4.24)上工作,而不是在我的实时服务器(Cent OS 5)上.我将服务器从5.3.9升级到5.5.15(并添加了mb和mcrypt功能,虽然这可能无关紧要),现在它可以工作了.
如果您在共享主机上,这可能没有用,但您可以询问他们是否可以重建PHP/Apache.
我解决了这个问题,改变了我php.ini
的:
variables_order = "GPCS"
Run Code Online (Sandbox Code Playgroud)
到:
variables_order = "GPCSE"
Run Code Online (Sandbox Code Playgroud)
默认情况下,PHP 不注册环境变量,因此此更改启用了它们。有趣的是,INPUT_SERVER
变量也恢复了工作!
只是两个附加信息,我正在使用 PHP 7.0.13,正如其他答案中所述,这个问题与PHP bug有关。
另一种选择是使用以下内容:
filter_var(getenv('REQUEST_METHOD'));
Run Code Online (Sandbox Code Playgroud)