Should I use PHP's filter_input to check the existance of Post Variables?

1.1k Views Asked by At

I have just started using netbeans to code PHP, and notice I am getting several code warnings stating that I should not be using superglobals such as $_POST. Instead I should be using filters.

I have done some research on filter_input and pretty much understand it, but have a question regarding one particular implimentation - checking the existance of a variable.

If my code currently looks like this:

if(isset($_POST['name'])) {
    ... do something ...
}

I know I can instead use:

if(filter_has_var(INPUT_POST, 'name')) {
    ... do something ...
}

But what if I want to simply check for the existance of any POST variables. i.e.:

if(isset($_POST)) {
    .. do something ...
}

Using the code below seems to work, but i don't want to go ahead and use it without understanding what this is doing, or if it is even correct.

if(filter_input(INPUT_POST)) {
    ... do something ...
}

Can anyone give some advice? Thanks.

1

There are 1 best solutions below

0
Carlos Vicent Domenech On

I'm sorry to be late, but I was looking fot the same response. I found that we can use filter_input_array, where the definition can be a filter constant such as FILTER_SANITIZE_STRING, because all $_POST elements are strings.

if (filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING)) {
    ... do something ...
}

And I have tested also your proposed code

if(filter_input(INPUT_POST)) {
    ... do something ...
}

and filter_input(INPUT_POST) always retun NULL and a Warning is displayed, because filter_input expects at least two parameters.