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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...