I'm wondering if it's possible to get a variable whether it is in POST or GET and then use filter_input() to sanitize it.
At first I thought that $var = filter_input(INPUT_POST | INPUT_GET, "var", FILTER_SANITIZE_STRING)
might work, however it doesn't and the PHP manual states you can only pass one type of input.
I also tried INPUT_REQUEST
, which strangely didn't work. The function recognises it (i.e. it doesn't throw up an error saying I've put something wrong in $input), yet it won't get any code. And yes, I know not to use INPUT_REQUEST
in a live environment, I was just purely testing to see if it would work.
Currently I do the following:
$var = filter_input(INPUT_POST, "var", FILTER_SANITIZE_STRING);
if(!$var) $var = filter_input(INPUT_GET, "var", FILTER_SANITIZE_STRING);
however with many things in PHP, there is often simpler way that will do it all for me in one command. I'm wondering if that is the case here, can I combine them into one check? I performed a cursory search on Google and couldn't even find any references to anyone trying this before, let alone a solution, so now I turn to you good folks.
It's considered bad practice if you don't know whether your input is in
GET
orPOST
. You should always know and not just randomly accept whatever.