0

I'm checking all input in php in both POST and GET and i'm turning them into html entities before anything else in my application.

problem is though, when i have multiple inputs of the same name (eg. multiple checkboxes ). my input checker nulls the array.

i want to check the POST and GET arrays and i want to check all the multiple inputs of the same name (ie. arrays ) within them.

can anyone suggest a piece of code to me ?

 // Input validation
 $_GET = array_map("input_check",$_GET);
 $_POST = array_map("input_check",$_POST);

 // Check input strings
 function input_check($arr)
 {

     return htmlentities($arr,ENT_QUOTES,'UTF-8'); 

 }
1
  • Not sure if you've seen my updated answer but it's pretty important to avoid array_map() Commented Jun 14, 2018 at 16:47

1 Answer 1

1

recursion! Welcome to the Welcome to the to the world of recursion!

array_map() will cause you to lose keys so let the function handle everything.

You will need to check and account for arrays:

// Input validation
$_GET = input_check($_GET);
$_POST = input_check($_POST);

// Check input strings
function input_check($arr)
{
    if(is_array($arr))
    {
        // Since this value is an array we need to apply this
        // function to each element inside the array and maintain
        // the original keys
        foreach($arr as $k=>$v)
        {
            $arr[$k] = input_check($v);
        }
    }
    elseif(is_string($arr))
    {
        $arr = htmlentities($arr,ENT_QUOTES,'UTF-8');
    }
    return $arr;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.