这是我的一种形式(PHP+MySQL,textarea 被 TinyMCE 取代)。它记录带有段落、项目符号、标题和文本对齐方式(右、左、居中和对齐)的描述。
提交后,记录显示为
<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() 之后,输出现在呈现为
我认为这是安全的,但从数据库中获取时在首页上看起来不太好。
如何呈现通过 htmlspecialchars 或 htmlentities 传递的输入?