微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在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,你的整数值将被转换为字符串.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐