php - sanitizer 输入但输出不符合预期

标签 php mysql output sanitization

这是我的一个表单(PHP+MySQL,textarea被TinyMCE取代)。它记录了带有段落、项目符号、标题和文本对齐方式(右对齐、左对齐、居中对齐和对齐)的描述。

enter image description here

提交后,记录显示为

<p style="text-align: justify;"><strong>Introduction</strong></p>
<p style="text-align: justify;">The death of the pixel leaves you with a flowing, magazine-quality canvas to design for. A canvas where curves are curves, not ugly pixel approximations of curves. A canvas that begins to blur the line between what we consider to be real and what we consider to be virtual.</p>
<p style="text-align: justify;">It wasn't too long ago that there was one set of rules for use of type on print and use of type on screen. Now that we have screens that are essentially print quality, we have to reevaluate these conventions.</p>
<p style="text-align: justify;">Web sites are transforming from boring fields of Arial to embrace the gamut of typographical possibilities offered by web fonts. Web fonts, combined with the style and layout options presented by the creative use of CSS and JavaScript offer a new world of typographic oppor</p>
<ol>
<li style="text-align: justify;">point 1</li>
<li style="text-align: justify;">point 2</li>
<li style="text-align: justify;">point 3</li>
</ol>

我读到您需要清理进入数据库的所有数据以避免 XSS 并开始寻找解决方案。

我找到的解决方案是使用“htmlspecialchars()”(来源:Lynda.com - 创建安全的 PHP 网站)。

所以,教程说我们需要在保存到数据库之前清理我们的输入并使用类似(示例代码)的东西

<?php
    if($_SERVER['REQUEST_METHOD'] === 'POST') {
        $category_description = $_POST['category_description'];
        echo $category_description;
        echo '<br><br>';
        echo htmlspecialchars($category_description);
        echo '<br><br>';
        echo htmlentities($category_description);
        echo '<br><br>';
        echo strip_tags($category_description);

    }
?>

避免XSS

我到这里为止。 htmlspecialchars() 函数将一些预定义的字符转换为 HTML 实体,htmlentities() 将字符转换为 HTML 实体,而 strip_tags() 则完全删除所有标签。

但是在使用 htmlspecialchars()、htmlentities() 和 strip_tags() 之后,输出现在呈现为

enter image description here

我认为这是安全的,但从数据库中获取时在首页上看起来不太好。

如何渲染通过 htmlspecialchars 或 htmlentities 传递的输入?

最佳答案

我的建议是构建一个函数来清理所有文本输入和一个函数来检查来自数据库或任何其他来源的所有输出,如下所示:

<?php
// filter for user input
function filterInput($content)
{
    $content = trim($content);
    $content = stripslashes($content);

    return $content;
}

//filter for viewing data
function filterOutput($content)
{
    $content = htmlentities($content, ENT_NOQUOTES);
    $content = nl2br($content, false);

    return $content;
}

根据您的策略,您可能会向过滤器添加额外功能或删除一些功能。但是你在这里拥有的功能足以保护你免受 XSS 攻击。

编辑:除了上述功能,this answer也可能与您的部分网站保护相关。

不同方法的引用:

查看以下链接也是一个好主意:

重要的是,了解前 10 大风险并了解更多信息是件好事。

关于php - sanitizer 输入但输出不符合预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33411455/

相关文章:

java - 打印格式

php - Imagick 在使用 PHP 将 SVG 转换为 JPG 时不使用自定义字体

php - $array[] = $value 或 array_push($array, $value) 在 PHP 中哪个更快?

mysql - 从 MySQL 中的循环引用表中获取所有 parent

php - mysql_query() 返回一些奇怪的东西

java - 反向Map.key、map.value输出

php - 实现弹出窗口

php - Symfony2 表单在提交时编码密码

mysql - 从 mysql 5.7 中的 json 数组中提取键

R:Sys.setlocale() 的静音输出