array_map() 和 call_user_func_array()

 array_map() 和 call_user_func_array()

 

下段代码的地址:http://cn2.php.net/manual/en/function.sprintf.php#93156

 

I created this function a while back to save on having to combine mysql_real_escape_string onto all the params passed into a sprintf. it works literally the same as the sprintf other than that it doesn't require you to escape your inputs. Hope its of some use to people


<?php
function mressf()
{
    $args = func_get_args();
    if (count($args) < 2)
        return false;
    $query = array_shift($args);
    $args = array_map('mysql_real_escape_string', $args);
    array_unshift($args, $query);
    $query = call_user_func_array('sprintf', $args);
    return $query;
}
?>

Regards
Jay

Jaygilford.com

 

说明:

这段代码的用意是对 mysql 的查询语句中所出现的参数进行转义操作,以避免数据库注入攻击。

这段函数,对参数数目的要求是必须大于2个,第一个是 含 sprintf 格式化语法的字符串,后面则跟需格式化的参数值。

$query = array_shift($args);  // 取出传入的第一个参数(SQL语句),$args 只留下其它SQL语句对应的参数

$args = array_map('mysql_real_escape_string', $args); // 对数组中的所有值,调用 mysql_real_escape_string 分别进行转义处理,再返回对应的数组

array_unshift($args, $query);  // 将 SQL 语句放回到 $args 数组中,成为数组的第一项。

$query = call_user_func_array('sprintf', $args); // 再调用函数 sprintf ,并将数组作为传入参数。

 

使用例:

printf(mressf("select * from users where username='%s' and password='%s'", 'username', "password' OR '1'='1"));

输出应该是:(注:未验证)

select * from users where username='username' and password='password\' OR \'1\'=\'1'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值