Php 正在清理输入,但输出不符合预期

Php 正在清理输入,但输出不符合预期,php,mysql,output,sanitization,input-sanitization,Php,Mysql,Output,Sanitization,Input Sanitization,这是我的一个表单(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

这是我的一个表单(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>

简介

像素的消亡给你留下了流动的、杂志质量的画布来设计。曲线是曲线的画布,而不是曲线的丑陋像素近似。一个画布开始模糊我们认为是真实的和我们认为是虚拟的。

网站正在从乏味的Arial字段转变为包含Web字体提供的各种排版可能性。Web字体,结合CSS和JavaScript的创造性使用提供的样式和布局选项,提供了一个新的排版世界

第1点 第2点 第3点
我了解到您需要清理进入数据库的任何数据,以避免XSS,并开始寻找解决方案

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

因此,本教程说,我们需要在保存到数据库之前对输入进行清理,并使用(示例代码)


要避免XSS

我一直到这里。函数的作用是:将一些预定义的字符转换为HTML实体,将字符转换为HTML实体,并删除所有标记

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

我相信这是安全的,但从数据库中获取时在头版上看起来并不好


如何呈现通过htmlspecialchars或htmlentities传递的输入?

我不确定这是否是解决问题的正确方法,但我在php手册中找到了一个函数“htmlspecialchars\u decode()。手册上说它与“htmlspecialchars()”完全相反。我试过了,效果很好


html_entity_decode()函数与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;
}
我使用了一个PHP过滤器库来清理HTML输入


您是否尝试过echo@Puyasarmadani,我想我找到了解决方案。
<?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;
}