可能重复:
在 PHP 中停止 SQL 注入的最佳方法
我正在使用 PHP 创建一个网站,该网站使用 MySQL 数据库并处理来自 URL 的表单和变量。这些变量用于动态构造 SQL 查询字符串。所以我需要一个强大的解决方案来确保没有人在尝试 SQL 注入等。我的一个朋友说我真的应该只使用存储过程来访问数据库,但这并不可行,因为我使用的主机没有不允许这些。
这是我正在使用的代码(它是包装数据库命令的类的一部分):
...
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.");
}
}
...
这个功能足够强大吗?我应该做其他事情吗?