我使用这个函数来防止SQL注入
function cleanQuery($string)
{
if(get_magic_quotes_gpc()) // prevents duplicate backslashes
{
$string = stripslashes($string);
}
if (phpversion() >= '4.3.0')
{
$string = mysql_real_escape_string($string);
}
else
{
$string = mysql_escape_string($string);
}
return htmlentities($string);
}
我就是这样用的
$sql = "select * from news where id = ".cleanQuery($id)." ";
调用page.php时查询应该是安全的?id=1
但是,当添加'到URL的末尾时,比如page.php?id=1'它给出错误
警告:mysql_fetch_object():提供的参数不是有效的mysql结果资源
这意味着网页仍然存在漏洞,我想,有没有人能解决?
最佳答案
使用PDO准备好的语句。当您参数化查询中的每个外部变量时,准备好的语句具有防止所有形式的参数注入的良好特性。也就是说,要小心数据类型转换。
关于php - PHP MySQL漏洞,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5085578/