php 使用 bigint,如何在php中使用bigint来防止sql注入?

我正在使用php并在mysql服务器上运行SQL查询.

为了防止sql注入,我使用的是mysql_real_escape_string.

我也使用(int)进行数字转换,方式如下:

$desired_age = 12;

$query = "select id from users where (age > ".(int)$desired_age.")";

$result = mysql_query($query);

那工作.

但是,当变量包含更大的数字时,由于它们大于int,因此它们会失败.

$user_id = 5633847511239487;

$query = "select age from users where (id = ".(int)$user_id.")";

$result = mysql_query($query);

// this will not produce the desired result,

// since the user_id is actually being cast to int

除了使用mysql_real_escape_string之外,是否还有另一种方法来强制转换(如BIGINT)sql注入预防?

解决方法:

你可以使用类似的东西:

preg_replace('/[^0-9]/','',$user_id);

替换字符串中的所有非数字符号.

但实际上没有必要这样做,只需使用mysql_real_escape_string(),因为一旦构建了$query,你的整数值将被转换为字符串.

标签:php,mysql,sql-injection,casting,bigint

来源: https://codeday.me/bug/20190713/1452124.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值