PHP filter_input(INPUT_SERVER, 'REQUEST_METHOD') returns null?

后端 未结 5 2135
一整个雨季
一整个雨季 2021-01-01 18:20

Why does this line return null in my live server?

filter_input(INPUT_SERVER, \'REQUEST_METHOD\');

The live server is p

5条回答
  •  囚心锁ツ
    2021-01-01 18:55

    So the problem/bug is this:

    filter_input() doesn't work with INPUT_SERVER or INPUT_ENV when you use FASTCGI

    The bug has been known for years and I found nothing saying it was addressed. I found several work-arounds but no complete solution so I plopped the best work-around into this helper function for a project-wide solution. To provide some level of security and avoid train wrecks, the function falls back to filter_var() where filter_input() fails. It uses the same format as the native filter_input() function for easy integration into projects and easy future removal should the bug ever be fixed.

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

    This seems the best solution. Please let me know if it contains errors that might cause issues.

提交回复
热议问题