【发布时间】:2009-05-19 17:12:48
【问题描述】:
我正在使用 PHP 创建一个网站,该网站使用 MySQL 数据库并处理来自 URL 的表单和变量。这些变量用于动态构造 SQL 查询字符串。所以我需要一个强大的解决方案来确保没有人尝试 SQL 注入等。我的一个朋友说我真的应该只使用存储过程来访问数据库,但这并不可行,因为我使用的主机没有'不允许这些。
这是我正在使用的代码(它是包装 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.");
}
}
...
这个函数足够健壮吗?我应该做点别的吗?
【问题讨论】:
-
你为什么不使用参数化查询?
-
什么是参数化查询,有什么好处?也许我应该说清楚我已经很长时间没有在线使用 SQL 数据库了。
-
查看 PDO,如果您想进行参数化查询。 php.net/pdo
标签: php post get sanitization