Skip to content

Instantly share code, notes, and snippets.

@fhefh2015
Created March 9, 2016 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fhefh2015/9edc71b8ceebf724ae02 to your computer and use it in GitHub Desktop.
Save fhefh2015/9edc71b8ceebf724ae02 to your computer and use it in GitHub Desktop.
防XSS 防SQL注入的代码
/**
* 过滤参数
* @param string $str 接受的参数
* @return string
*/
static public function filterWords($str)
{
$farr = array(
"/<(\\/?)(script|i?frame|style|html|body|title|link|meta|object|\\?|\\%)([^>]*?)>/isU",
"/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",
"/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|dump/is"
);
$str = preg_replace($farr,'',$str);
return $str;
}
/**
* 过滤接受的参数或者数组,如$_GET,$_POST
* @param array|string $arr 接受的参数或者数组
* @return array|string
*/
static public function filterArr($arr)
{
if(is_array($arr)){
foreach($arr as $k => $v){
$arr[$k] = self::filterWords($v);
}
}else{
$arr = self::filterWords($v);
}
return $arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment