Question

Why doesn't this work?

session_start();

print("<pre>".print_r($_POST['foo'],true)."</pre>");  // 'Bob     '

array_walk_recursive($_POST, function (&$val) { $val = trim($val); });
print("<pre>".print_r($_POST['foo'],true)."</pre>");  // 'Bob'

$_SESSION['foo'] = filter_input(INPUT_POST, 'foo', FILTER_SANITIZE_STRING);
print("<pre>".print_r($_SESSION['foo'],true)."</pre>");  // 'Bob     '
Was it helpful?

Solution

From the first comment in the manual:

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.

edit

Here's where you would add your call to trim():

$_SESSION['foo'] = trim(filter_input(INPUT_POST, 'foo', FILTER_SANITIZE_STRING));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top