现在的富文本编辑器,都有xss注入漏洞,想请教一下,如何防止xss注入?markdown编辑器先不考虑
服务器端清除xss攻击
string htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = "UTF-8" [, bool $double_encode = true ]]] )
例子:
<?php $str = "A 'quote' is <b>bold</b>" ; // Outputs: A 'quote' is <b>bold</b> echo htmlentities ( $str ); // Outputs: A 'quote' is <b>bold</b> echo htmlentities ( $str , ENT_QUOTES ); ?>
<?php $str = "\x8F!!!" ; // Outputs an empty string echo htmlentities ( $str , ENT_QUOTES , "UTF-8" ); // Outputs "!!!" echo htmlentities ( $str , ENT_QUOTES | ENT_IGNORE , "UTF-8" ); ?>
string htmlspecialchars ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = "UTF-8" [, bool $double_encode = true ]]] )
<?php $new = htmlspecialchars ( "<a href='test'>Test</a>" , ENT_QUOTES ); echo $new ; // <a href='test'>Test</a> ?>
还原显示可以使用htmlspecialchars_decode、html_entity_decode
string htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] )
<?php $str = "<p>this -> "</p>\n" ; echo htmlspecialchars_decode ( $str ); // 注意,这里的引号不会被转换 echo htmlspecialchars_decode ( $str , ENT_NOQUOTES ); ?>
以上过程会输出
<p>this -> "</p> <p>this -> "</p>
string html_entity_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = "UTF-8" ]] )
<?php $orig = "I'll \"walk\" the <b>dog</b> now" ; $a = htmlentities ( $orig ); $b = html_entity_decode ( $a ); echo $a ; // I'll "walk" the <b>dog</b> now echo $b ; // I'll "walk" the <b>dog</b> now ?>
评论(0)
登录 后参与评论。