I use filter_input_array
PHP function when sanitizing data inputs in my application. Consider example below:
public function acquireData()
{
$args = array(
'model' => FILTER_SANITIZE_STRING,
);
//function that sanitizes input from POST
$output = filter_input_array(INPUT_POST, $args);
return $output;
}
It works fine when I read live POST data from a web browser. But when I want to pass in another array for the purposes of testing I struggle to find a solution.
i.e. using my own array does not work and produces empty $output
:
$myInputArray = array('model' => 'ABC'); //with same parameters as $_POST
$output = filter_input_array($myInputArray, $args);
//$output is null;
Note
I want to specifically use INPUT_POST
construct. Or get an answer "it is not possible" or "you can only seek work-arounds".
i.e. using filter_var_array
is a work-around, because it does not use INPUT_POST
. Example work-around below:
$output = filter_var_array($_POST, $args);
Until I know better, there only seems to be a work-around:
$output = filter_var_array($_POST, $args)
In other words, when you want to test using your own array, either write another function to sanitize inputs for your own array, or use filter_var_array
instead of filter_input_array
.
filter_input_array
appears to be just a convenience function. Instead of providing the specific variable containing the inputs, you provide a constant that specifies one of a standard set of global variables that can be filtered automatically. So
filter_input_array(INPUT_POST, $args);
is equivalent to:
filter_var_array($_POST, $args);