【问题标题】:Extra Sanitisation When Dealing With Fetched Data处理获取数据时的额外清理
【发布时间】: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


【解决方案1】:

您当前的代码是正确且安全的。你不需要其他任何东西。

没有输入净化之类的东西。 准备好的语句不会清理数据。它们可以防止 SQL 注入,因为数据是与查询分开发送的。

htmlspecialchars() 防止 XSS。 在将数据输出到 HTML 时使用它。不要在输入时使用它,因为它会损坏您的数据。

切勿以任何方式修改密码!。直接在输入上使用password_hash() 并将其保存到数据库中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 2011-07-01
    • 1970-01-01
    • 2012-04-26
    • 2011-06-23
    相关资源
    最近更新 更多