PHP防止XSS攻击函数
2017-7-19 dingshangchao php开发
XSS 全称为 Cross Site Scripting,用户在表单中有意或无意输入一些恶意字符,从而破坏页面的表现。对于PHP开发者来说,如何去防范XSS攻击呢?可以用如下函数:
<?php
function clean_xss(&$string,$low=false){
if(!is_array($string)){
$string = trim($string);
$string = strip_tags($string);
$string = htmlspecialchars($string);
}
if($low) return true;
$string = str_replace(array('"',"\\","'","/","..","../","./","//"), '', $string);
$no = '/%0[0-8bcef]/';
$string = preg_replace($no, '', $string);
$no = '/%1[0-9a-f]/';
$string = preg_replace($no, '', $string);
$no = '/[\x00-\x08\x0B\x0C\x0E]-\x1F\x7F]+/S';
$string = preg_replace($no, '', $string);
return true;
$keys = array_keys($string);
foreach($keys as $key){
clean_xss($string[$key]);
}
}
$str = 'phpddt.com<meta http-equiv="refresh" content="0">';
clean_xss($str);
echo $str;
标签: XSS