简体   繁体   English

检查php输入中的数组值

[英]checking input in php for array values

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. 我正在POST和GET中检查php中的所有输入,然后将它们转换为html实体,然后再在应用程序中进行其他操作。

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. 我想检查POST和GET数组,我想检查其中所有具有相同名称的所有多个输入(即arrays)。

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'); 

 }

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. array_map()将导致您丢失密钥,因此让函数处理所有事情。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM