yeh*_*uda 3 php mysql casting sql-injection bigint
我正在使用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);
Run Code Online (Sandbox Code Playgroud)
那工作.
但是,当变量包含更大的数字时,由于它们大于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
Run Code Online (Sandbox Code Playgroud)
除了使用mysql_real_escape_string之外,是否还有另一种方法来强制转换(如BIGINT)sql注入预防?
Joh*_*nde 10
如果您自己生成用户ID,则无需为MySQL投射,因为没有SQL注入或其他字符串问题.
如果它是用户提交的值,则使用filter_var()(或is_numeric())验证它是数字而不是字符串.