可能重复:
Best way to stop SQL Injection in PHP从表单/ URL清理POST/GET变量的最佳方法是什么?
我创建使用PHP,使得使用MySQL数据库和表格处理和变量从URL一个网站。正在使用这些变量来动态构建SQL查询字符串。所以我需要一个强大的解决方案,以确保没有人正在尝试SQL注入等。我的一个朋友说,我应该只使用存储过程来访问数据库,但这不是真的可行,因为我使用的主机doesn不允许这些。
这里是我正在使用的代码(这是一个类的一部分来包装DB命令):
...
public function Sanitize($Variable)
{
if(is_resource($this->ServerConnection))
{
$Variable = str_replace(";", "", $Variable);
if(get_magic_quotes_gpc())
{
if(ini_get('magic_quotes_sybase'))
{
$Variable = str_replace("''", "'", $Variable);
}
else
{
$Variable = stripslashes($Variable);
}
}
return mysql_real_escape_string($Variable, $this->ServerConnection);
}
else
{
$this->PrintError("The Sanitize function is not available as there is no server connection.");
}
}
...
这是功能强大的还不够吗?我应该做其他事吗?
为什么不使用参数化查询? – dave4420 2009-05-19 17:18:53