首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >清除输入,但不按预期输出

清除输入,但不按预期输出
EN

Stack Overflow用户
提问于 2015-10-29 10:16:56
回答 3查看 493关注 0票数 2

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

一旦提交,记录将显示为

代码语言:javascript
复制
<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 )。

因此,本教程说,在保存到数据库之前,我们需要清理输入,并使用类似于(示例代码)的内容。

代码语言:javascript
复制
<?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实体传递的输入?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-10-29 19:18:11

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

代码语言:javascript
复制
<?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的攻击。

编辑:,除了上面的功能,这个答案也可能在你的网站保护的一部分相关。

提及不同的方法:

  • trim:http://php.net/manual/en/function.trim.php
  • 条纹斜杠:http://php.net/manual/en/function.stripslashes.php
  • htmlentities实体:http://php.net/manual/en/function.htmlentities.php
  • nl2br:http://php.net/manual/en/function.nl2br.php

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

更重要的是,意识到前十大风险并了解更多有关它的信息是很好的。

票数 1
EN

Stack Overflow用户

发布于 2015-10-29 15:33:52

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

html_entity_decode()函数与htmlentities实体()相反。

票数 0
EN

Stack Overflow用户

发布于 2015-10-30 12:31:42

我使用了HTMLPurifier,这是一个PHP库,用于清理HTML输入。

用HTML内容净化POST内容

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33411455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档