php提交代码,允许用户在PHP中提交HTML

这篇博客讨论了作者从使用黑名单方法过滤用户提交的HTML转向白名单方法的过程。当前的黑名单方法已经运行两年,没有出现问题,但作者寻求与StackOverflow上的C方法类似的PHP实现来创建更安全的白名单过滤。文章涵盖了黑名单方法的细节,并询问如何将其转换为白名单过滤策略。
摘要由CSDN通过智能技术生成

我想允许很多用户为用户配置文件提交HTML,我目前试图过滤掉我不想要的内容,但我现在想改变并使用白名单方法。

这是我目前的非白名单方法

function FilterHTML($string) {

if (get_magic_quotes_gpc()) {

$string = stripslashes($string);

}

$string = html_entity_decode($string, ENT_QUOTES, "ISO-8859-1");

// convert decimal

$string = preg_replace('/(\d+)/me', "chr(\\1)", $string); // decimal notation

// convert hex

$string = preg_replace('/([a-f0-9]+)/mei', "chr(0x\\1)", $string); // hex notation

//$string = html_entity_decode($string, ENT_COMPAT, "UTF-8");

$string = preg_replace('#(&\#*\w+)[\x00-\x20]+;#U', "$1;", $string);

$string = preg_replace('#(]+[\s\r\n\"\'])(on|xmlns)[^>]*>#iU', "$1>", $string);

//$string = preg_replace('#(&\#x*)([0-9A-F]+);*#iu', "$1$2;", $string); //bad line

$string = preg_replace('#/*\*()[^>]*\*/#i', "", $string); // REMOVE /**/

$string = preg_replace('#([a-z]*)[\x00-\x20]*([\`\'\"]*)[\\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iU', '...', $string); //JAVASCRIPT

$string = preg_replace('#([a-z]*)([\'\"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iU', '...', $string); //VBSCRIPT

$string = preg_replace('#([a-z]*)[\x00-\x20]*([\\\]*)[\\x00-\x20]*@([\\\]*)[\x00-\x20]*i([\\\]*)[\x00-\x20]*m([\\\]*)[\x00-\x20]*p([\\\]*)[\x00-\x20]*o([\\\]*)[\x00-\x20]*r([\\\]*)[\x00-\x20]*t#iU', '...', $string); //@IMPORT

$string = preg_replace('#([a-z]*)[\x00-\x20]*e[\x00-\x20]*x[\x00-\x20]*p[\x00-\x20]*r[\x00-\x20]*e[\x00-\x20]*s[\x00-\x20]*s[\x00-\x20]*i[\x00-\x20]*o[\x00-\x20]*n#iU', '...', $string); //EXPRESSION

$string = preg_replace('#*\w+:\w[^>]*>#i', "", $string);

$string = preg_replace('#?t(able|r|d)(\s[^>]*)?>#i', '', $string); // strip out tables

$string = preg_replace('/(potspace|pot space|rateuser|marquee)/i', '...', $string); // filter some words

//$string = str_replace('left:0px; top: 0px;','',$string);

do {

$oldstring = $string;

//bgsound|

$string = preg_replace('#*(applet|meta|xml|blink|link|script|iframe|frame|frameset|ilayer|layer|title|base|body|xml|AllowScriptAccess|big)[^>]*>#i', "...", $string);

} while ($oldstring != $string);

return addslashes($string);

}

上面的工作非常好,我在使用它2年后从来没有遇到过任何问题,但是对于白名单方法,StackOverflows C方法和PHP有什么相似之处吗?

http://refactormycode.com/codes/333-sanitize-html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值