【发布时间】:2021-07-19 09:57:44
【问题描述】:
我从数据库中获取了一些数据(在进入数据库的过程中使用准备好的语句对其进行了清理)。在使用这些数据时,我认为我必须使用 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 和 XSS 注入是不同的问题。您需要为每个
htmlspecialchars提供不同的解决方案,以防止 XSS 注入。仅用于输出。你不应该输出密码,如果你这样做了,它应该是乱码,因为它是散列的。 -
嗨@user3783243 这就是我的想法。我想要求确认,因为我是 PHP 新手。感谢您抽出宝贵时间发表评论。
标签: php security prepared-statement sanitization