Php 防止XSS(理想功能)? 函数xap($in,$format=false){ 如果($format=='html'){ //html格式 $in=preg_replace('/([^
Php 防止XSS(理想功能)? 函数xap($in,$format=false){ 如果($format=='html'){ //html格式 $in=preg_replace('/([^,php,antixsslibrary,Php,Antixsslibrary,htmlentities()将保护您免受XSS攻击 您的代码看起来还希望允许使用一些HTML代码。您仍然应该运行htmlentities(),然后运行str\u replace()以允许使用所需的一些HTML标记 function xap ($in, $format=false) { if ($format == 'html') { //Делаем безопасный html $in = preg_replace('/(<(link|script|iframe
htmlentities()
将保护您免受XSS攻击
您的代码看起来还希望允许使用一些HTML代码。您仍然应该运行htmlentities()
,然后运行str\u replace()
以允许使用所需的一些HTML标记
function xap ($in, $format=false) {
if ($format == 'html') {
//Делаем безопасный html
$in = preg_replace('/(<(link|script|iframe|object|applet|embed).*?>[^<]*(<\/(link|script|iframe|object|applet|embed).*?>)?)/i', '', $in); //Удаляем стили, скрипты, фреймы и flash
$in = preg_replace('/(script:)|(expression\()/i', '\\1 ', $in); //Обезвреживаем скрипты, что остались
$in = preg_replace('/(onblur|onchange|onclick|ondblclick|onfocus|onkeydown|onkeypress|onkeyup|onload|onmousedown|onmousemove|onmouseout|onmouseover|onmouseup|onreset|onselect|onsubmit|onunload)=?/i', '', $in);
$in = preg_replace('/((src|href).*?=.*?)(http:\/\/)/i', '\\1redirect/\\2', $in);
return $in;
} else {
return htmlentities($in);
}
}
echo xap($text); //for read
echo xap($text, "html"); //for read html tags
应该足够安全
您忘记了一个错误。=>
没有“理想代码”这样的东西。有很多讨厌的方法可以绕过这种XSS过滤器,包括滥用UTF-7等鲜为人知的编码。我会寻找一个公开记录、知名度高、同行评审的库,而不是相信一位作者提供“理想代码”。
htmlspecialchars($str, ENT_QUOTES, 'UTF-8');