Php: Reading $_POST-array with filter_input returns empty array

897 views Asked by At

I tried to submit form data as an array (“newData”) arriving at my php application in the structure

$_POST['newData'] = array(
    1 => array( p1 => 'a', p2 => 'ae', /*etc.*/ ),
    2 => array( p1 =>  /*etc.*/ )
)

which told me the print_r()-command.

Because I usually call form data by filter_input(), I wrote into my program:

$newData = filter_input(INPUT_POST, 'newData',
    FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);

But this does not contain any value. Did I use the filter command in a crong way or could there be some switch in the php.ini I do not know? Other program where I filter input arrays the same way run on another server, that's why I think this might be a problem.

1

There are 1 answers

0
Roelof On BEST ANSWER

The reason that filter_input doesn't see the data you added to the $_POST superglobal, is that filter_input seems to check the raw data, not the parsed data that is in $_POST. The same thing goes for $_GET

print( filter_input(INPUT_GET, 'foo') ); // Prints "bar"

$_GET['foo'] = 'foo'; // Sets the $_GET superglobal, but does not change the raw request data

print( filter_input(INPUT_GET, 'foo') ); // Still returns bar

I know you probably already solved the issue, but it took me quite some time to figure out and I'd like to help others out of their struggle.