【问题标题】:Having issue with htmlspecialchars, where is the correct place to put it?遇到 htmlspecialchars 问题,正确的放置位置在哪里?
【发布时间】:2017-03-11 22:14:07
【问题描述】:
$stmt = $conn->prepare("INSERT INTO chatbox (username, message)
    VALUES (:username, :message)");
    $stmt->bindParam(':username', $username);
   $stmt->bindParam(':message', $message);

    $username = $_POST['username'];
    $message = $_POST['message'];
    $stmt->execute();
    ?>

在这种情况下我应该把 htmlspecialchars() 放在哪里,请帮帮我?

【问题讨论】:

    标签: php html pdo


    【解决方案1】:

    这里没有。始终尝试将“原始”(见下文我所说的“原始”)数据放入您的数据库中。仅当您想显示数据库中的数据时才使用 htmlspecialchars。

    "raw" as in 对数据库进行了清理和安全处理,但没有以某种格式(例如 HTML)的方式被触及

    //编辑:
    因此,为了正确使用 htmlspecialchars,假设您在从数据库接收到该消息后回显该消息:

    echo htmlspecialchars($message);
    

    【讨论】:

    • 是正确的,但为了安全起见,请记住首先验证您的字符串。
    • 这是我输出数据的代码: $result = $pdo->prepare("SELECT * FROM chatbox ORDER BY chat_id DESC LIMIT 4"); $结果->执行(); while ($row = $result->fetch(PDO::FETCH_ASSOC)) { $username = $row['username']; $message = $row['message']; ?> 我应该把 htmlspecialchars() 放在哪里?
    • 你能在你最初的问题中写下这段代码吗?与此同时,我编辑了我的答案;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 2011-07-02
    • 2011-04-26
    相关资源
    最近更新 更多