PHP filter_input(INPUT_SERVER, 'REQUEST_METHOD' ) 返回 null?

标签 php filter-input

为什么此行在我的实时服务器中返回 null

filter_input(INPUT_SERVER, 'REQUEST_METHOD');

实时服务器是php5.5.9

我错过了什么吗?

我以为是用来替换下面的全局方法的?

$_SERVER['REQUEST_METHOD'];

一些代码,

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();
        }
    }

最佳答案

所以问题/错误是这样的:

当您使用 FASTCGI 时,filter_input() 无法与 INPUT_SERVERINPUT_ENV 一起使用

这个错误已经存在很多年了,但我没有发现任何消息表明它已得到解决。我找到了几种解决方法,但没有完整的解决方案,因此我将最佳的解决方法放入此帮助函数中,以实现项目范围的解决方案。为了提供一定程度的安全性并避免火车失事,该函数回退到filter_var(),其中filter_input() 失败。它使用与 native 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;
    }
}

这似乎是最好的解决方案。如果它包含可能导致问题的错误,请告诉我。

关于PHP filter_input(INPUT_SERVER, 'REQUEST_METHOD' ) 返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25232975/

相关文章:

php - 什么是 zend 扩展?

php - 如何在数据库中保存文件路径并在详细页面中显示路径

php - 如何检查句子中是否存在单词

php - filter_var 和 filter_input 在输入数据验证方面的区别

zend-framework - Zend Framework 过滤器输入 ENUM

PHP filter_require_array 失败

php - MySQLi 不更新我的查询

javascript - 如何更快地加载通过ajax获取的数据?

php - filter_input 和 mysqli_real_escape_string 用于整数

php - FILTER SANITIZE 与 FILTER VALIDATE,有什么区别 - 使用哪个?