Simple your code should look like this:
...
if ( is_array($userInput) )
{
foreach ( $userInput as $key => $value )
{
return sanitize( $value ); //<-------- needed to return it or
else its not recurssive
}
}
else
{
...
.
Should work, not tested.
Best,
Yoed
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Friday, October 08, 2004 5:15 PM
To: [EMAIL PROTECTED]
Subject: [PHP] Recursion to sanitize user input
I'm trying to sanitize my user input. My sanitize function does not work if
I send a variable that's an array. I'm using recursion to go through the
array. The example below shows that $_POST['city'] works but $_POST['user']
doesn't work. The array comes back blank.
Anyone see what's wrong with my code?
OUTPUT:
Array
(
[city] => New York
[user] =>
)
CODE:
<?php
function sanitize($userInput = '')
{
if ( is_array($userInput) )
{
foreach ( $userInput as $key => $value )
{
sanitize( $value );
}
}
else
{
if ( get_magic_quotes_gpc() )
{
return trim( $userInput );
}
else
{
return trim( addslashes($userInput) );
}
}
}
$_POST['city'] = 'New York';
$_POST['user']['firstName'] = 'Bob';
$_POST['user']['lastName'] = 'Smith';
$_POST['user']['country'] = 'USA';
foreach ( $_POST as $key => $value )
{
$_POST[$key] = sanitize( $value );
}
echo '<pre>';
echo print_r($_POST);
echo '</pre>';
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php