【问题标题】:PHP add html tags white list for sanitize inputPHP 添加 html 标签白名单以清理输入
【发布时间】:2015-10-15 19:07:33
【问题描述】:

我找到了GUMP 用于清理和验证数据输入的类,它的工作原理如下:

# Note that filters and validators are separate rule sets and method calls. There is a good reason for this.

require "gump.class.php";

$gump = new GUMP();

$_POST = $gump->sanitize($_POST); // You don't have to sanitize, but it's safest to do so.

$gump->validation_rules(array(
    'title'      => 'required',
    'story'      => 'required'

));

$gump->filter_rules(array(
    'title'    => 'trim|sanitize_string',
    'story'    => 'trim|sanitize_string',
));

$validated_data = $gump->run($_POST);

if($validated_data === false) {
    echo $gump->get_readable_errors(true);
} else {
    print_r($validated_data); // validation successful
}

实际上,这很有效,可以清理所有输入数据。对于story 字段需要添加像<p><img><table> 这样的html 标签,但是这个类会清理所有$_POST 并删除所有html 标签。

我不知道如何添加白名单(<p><img><table>) 进行消毒?!如何为html标签添加白名单?

【问题讨论】:

  • 欢迎来到 Stack Overflow。我已经编辑了您的帖子以修复英语的使用和拼写错误。很高兴您发现了如何标记代码。

标签: php mysql sanitize


【解决方案1】:

quick look at the class 表明你不能这样做。

另一种解决方案是使用内置函数strip_tags()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多