这是我的表单之一(PHP+MySQL,由TinyMCE取代的textarea )。它用段落、符号、标题和文本对齐(右、左、中和对齐)记录描述。

一旦提交,记录将显示为
<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。
我得到它直到这里。函数将一些预定义的字符转换为htmlentities,HTML ()将字符转换为htmlentities,而strip_tags()则完全删除任何标记。
但是在使用htmlspecialchars()、htmlentities()和strip_tags()之后,输出现在呈现为

我认为这是安全的,但从数据库获取时头版看起来并不好。
如何呈现已通过htmlspecialchars或How实体传递的输入?
发布于 2015-10-29 19:18:11
我的建议是构建一个函数来净化所有文本输入,并构建一个函数来检查来自数据库或任何其他来源的所有输出,如下所示:
<?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的攻击。
编辑:,除了上面的功能,这个答案也可能在你的网站保护的一部分相关。
提及不同的方法:
查看以下链接也是一个好主意:
更重要的是,意识到前十大风险并了解更多有关它的信息是很好的。
发布于 2015-10-29 15:33:52
我不确定这是否是解决问题的正确方法,但我在php手册中找到了一个函数"htmlspecialchars_decode()“。手册说,它所做的与"htmlspecialchars()“正好相反。我试过了,效果很好。
html_entity_decode()函数与htmlentities实体()相反。
发布于 2015-10-30 12:31:42
我使用了HTMLPurifier,这是一个PHP库,用于清理HTML输入。
https://stackoverflow.com/questions/33411455
复制相似问题