我找到了 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
需要添加 html 标签的字段,如 <p><img><table>
但是这个类 sanitizer 所有$_POST
并删除所有 html 标签。
我找不到如何添加白名单 ( <p><img><table>
) 进行清理?!如何为 html 标签添加白名单?
最佳答案
A quick look at the class表明你不能那样做。
另一种解决方案是使用内置函数 strip_tags()
.
关于PHP 添加 html 标签白名单以清理输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33156530/