参见英文答案 >
Do htmlspecialchars and mysql_real_escape_string keep my PHP code safe from injection? 6个
我试图想出一种方法,用一个函数有效地轻松清理所有POST和GET变量.这是函数本身:
//clean the user's input
function cleanInput($value, $link = '')
{
//if the variable is an array, recurse into it
if(is_array($value))
{
//for each element in the array...
foreach($value as $key => $val)
{
//...clean the content of each variable in the array
$value[$key] = cleanInput($val);
}
//return clean array
return $value;
}
else
{
return mysql_real_escape_string(strip_tags(trim($value)), $link);
}
}
以下是可以称之为的代码:
//This stops SQL Injection in POST vars
foreach ($_POST as $key => $value)
{
$_POST[$key] = cleanInput($value, $link);
}
//This stops SQL Injection in GET vars
foreach ($_GET as $key => $value)
{
$_GET[$key] = cleanInput($value, $link);
}
对我来说,这似乎应该有效.但由于某种原因,它不会从表格中的一些复选框返回数组.他们一直空白.
我已经在没有上述功能的情况下测试了我的代码并且工作正常,我只想在那里增加一些安全性.
谢谢!