Skip to content

Instantly share code, notes, and snippets.

@medeirosinacio
Created June 24, 2021 21:21
Show Gist options
  • Save medeirosinacio/c9cdb36f5922a280d624cf2c21269a0b to your computer and use it in GitHub Desktop.
Save medeirosinacio/c9cdb36f5922a280d624cf2c21269a0b to your computer and use it in GitHub Desktop.
FIX_BUG: filter_input() doesn't work with INPUT_SERVER or INPUT_ENV when you use FASTCGI
/**
* FIX_BUG: filter_input() doesn't work with INPUT_SERVER or INPUT_ENV when you use FASTCGI
* @param $type
* @param $variable_name
* @param int $filter
* @param null $options
* @return mixed|null
* @package https://stackoverflow.com/questions/25232975/php-filter-inputinput-server-request-method-returns-null/25385553
*/
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;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment