问题描述
我从数据库中获取了一些数据(在使用准备好的语句进入数据库的过程中对其进行了清理)。使用这些数据时,我想我必须使用 htmlspecialchars() 函数,但是使用包含特殊字符的密码会破坏代码,因为它显然将 <
变成了 html 实体。
我的想法是否正确,如果代码在进入数据库时经过清理,并且没有物理输出到 html 页面,我不必为下面的内容添加任何额外的安全性?
我最初将代码包装在 htmlspecialchars() 中的 while
循环中,例如$db_id = htmlspecialchars($row['ID']);
这就是我发现它破坏代码的方式。
if (isset($_POST['login'])) {
$email = $_POST['email'];
$stmt = $connection->prepare("SELECT * FROM users WHERE email = ? ");
$stmt->bind_param("s",$email );
$stmt->execute();
$result = $stmt->get_result();
// assign columns from the database to variables
while ($row = MysqLi_fetch_array($result)) {
$db_id = $row['ID'];
$db_firstname = $row['firstname'];
$db_email = $row['email'];
$db_password = $row['password'];
}
$stmt->close();
$connection->close();
// code will go here that checks if the email and password match and then create a $_SESSION
}
解决方法
您当前的密码正确且安全。您不需要其他任何东西。
没有输入清理这样的东西。 准备好的语句不会清理数据。它们可以防止 SQL 注入,因为数据与查询分开发送。
htmlspecialchars()
防止 XSS。您在将数据输出到 HTML 时使用它。不要在输入时使用它,因为它会损坏您的数据。
切勿以任何方式修改密码!。直接在输入上使用 password_hash()
并将其保存到数据库中。