【发布时间】:2010-05-24 03:25:34
【问题描述】:
可能重复:
Do I have to use mysql_real_escape_string if I bind parameters?
我有一个与 MySQLi 安全相关的快速问题...
例如,看一下这段代码(从用户那里获取输入,对照数据库检查用户名/密码组合是否存在):
$input['user'] = htmlentities($_POST['username'], ENT_QUOTES);
$input['pass'] = htmlentities($_POST['password'], ENT_QUOTES);
// query db
if ($stmt = $mysqli->prepare("SELECT * FROM members WHERE username=? AND password = ?"))
{
$stmt->bind_param("ss", $input['user'], md5($input['pass'] . $config['salt']));
$stmt->execute();
$stmt->store_result();
// check if there is a match in the database for the user/password combination
if ($stmt->num_rows > 0)
{}
}
在这种情况下,我在表单数据上使用 htmlentities(),并使用 MySQLi 准备好的语句。我还需要使用 mysql_real_escape_string() 吗?
【问题讨论】:
-
我想我还应该问...除了使用 MySQLi 准备好的语句和 htmlentities() 之外,还有什么我应该做的,安全方面的吗?
-
这不是在编辑您的原始问题,但已经足够好了 :)
-
htmlentities用于向客户端显示。你不应该真的在数据库查询中使用它——它是多余的。 -
@Mark - 抱歉,这里是新用户。 :) 以后我会先搜索。
-
无需抱歉 :) 我们这样做是为了将来参考。