phpsuperglobalsauto-registration

Global registration issues when registering CLI params into GET


ran into a weird one here, maybe some of the more seniory of guys can help me come to a conclusion on what exactlies going on (already have a work around, but would like to know how to fix this as it seems it can affect other things)

So I have a line of code that will detect a CLI string (based on type[0]) and then proceed to convert and load CLI parameters into GET params.

parse_str(implode("&", array_slice($payload['args'], 1)), $_GET);

That works fine, you can test it with creating a file and issueing it something like

php - f test.php -- foo=bar bar=baz

With the contents of the file using the above line and just a print_r($_GET);

Well it get weird when you try to use this as a filter_input I've noticed but cannot figure out why

$filter = filter_input_array(INPUT_GET, [
   'email'     => FILTER_VALIDATE_EMAIL,
]);

print_r($filter); //empty

It also looks like trying to tack on anything to a GET request is forgone by filter_input_array in general, instance

http://localhost?request=foo

$_GET['email'] = 'Someguy@somplace.com';

    //print_r($_GET); // check

print_r(filter_input_array(INPUT_GET, [
   'email'     => FILTER_VALIDATE_EMAIL,
   'request'   => FILTER_SANITIZE_ENCODED,
])):

What DOES work though for unknown reasons is

$filter = filter_var_array($_GET, [
   'email'     => FILTER_VALIDATE_EMAIL,
]);

Makes little to no sense to me =(

I would LIKE to believe that our GET somehow has not registered into the global scope, but I'm lacking on figuring out why...

I am running PHP 5.5.8 for Saucy, input welcome


Solution

  • The functions filter_input_array() and filter_input() work on the actual input, not on the corresponding global variables.

    From a note in the documentation for filter_input:

    Note that this function doesn't (or at least doesn't seem to) actually filter based on the current values of $_GET etc. Instead, it seems to filter based off the original values.

    source