我有这种PHP功能
function insertData($username,$name,$password) {
}
以及其他一些如:
function updateData($color, $hair,$eye) {
}
等。
因为这些参数可以包含一些引号或双引号,如“或”
有没有办法对它们进行消毒,如
的sanitize(allFunctionargs)
无需写 消毒($ color); 消毒($ hair); 消毒($ eye);
我正在寻找最通用的函数来清理(剥离斜杠)函数的所有参数
此致
答案 0 :(得分:0)
您可以使用func_get_args:和foreach
function insertData($username,$name,$password) {
$args = func_get_args();
foreach ($args as $arg)
{
// sanitize and whatnot
}
}
答案 1 :(得分:0)
您不能只调用函数外部的函数来清理所有函数参数。你必须在这样的函数中这样做:
function foo(/* Polymorphic */){
// Sanitize the arguments
$arguments = sanitize(function_get_args());
// Proceed with the rest of the function
// $arguments will now be sanitized
}
function sanitize(/* Polymorphic */){
// Get the functions arguments
$arguments = function_get_args();
// If the arguments exist then proceed to sanitizing
if(is_array($arguments)){
foreach($arguments as $key=>$value){
$arguments[$key] = stripslashes($value);
}
}
// Return the sanitized array
return $arguments;
}
注意/* Polymorphic */
评论;这实际上意味着您可以提交任意数量的参数。这里唯一的问题是你不知道哪个参数是哪个,除非你这样提交:
// Call the foo function
foo(array(
'username' => $username,
'name' => $name,
'password' => $password
));
然后在foo函数中,你将能够像这样访问参数:
function foo(/* Polymorphic */){
// Sanitize the arguments
$arguments = sanitize(function_get_args());
// You only want the first item in the arguments array so remove the others...
$arguments = $arguments[0];
// Proceed with the rest of the function
echo 'Username: '.$arguments['username']."\n";
echo 'Name: '.$arguments['name']."\n";
echo 'Password: '.$arguments['password'];
}
如果您正在剥离MySQL(或任何其他数据库)的斜杠,那么我非常建议您查看MySQLi或PDO库和预处理语句。如果使用预准备语句,则无需进行剥离斜杠的工作。有关详细信息,请参阅以下链接:
答案 2 :(得分:0)
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
$array = func_get_args();
$array = stripslashes_deep($array);
print_r($array);