我使用这个函数来防止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时,查询应该是安全的,但是当添加‘像那个page.php?id=1一样的URL末尾’时,就会产生错误
警告: mysql_fetch_object():提供的参数不是有效的MySQL结果资源
这意味着页面仍然有一个漏洞,我认为,有谁有解决办法?
发布于 2011-02-23 00:33:13
使用PDO的准备语句。当您将查询中的每个外部变量参数化时,准备好的语句具有防止所有形式的参数注入的优点。这就是说,要小心数据类型转换。
发布于 2011-02-23 00:11:01
整数使用(int)
$sql = 'select * from news where id = '.(int) $_GET[id];
发布于 2011-02-23 00:07:47
如果您的id是数字的,最简单的解决方案就是简单地
$sql = "select * from news where id = '".intval($_GET['id'])."'";https://stackoverflow.com/questions/5085578
复制相似问题