首页 > 开发 > PHP > 正文

php 安全过滤函数代码

2024-05-04 22:07:35
字体:
来源:转载
供稿:网友

代码如下:
//安全过滤输入[jb]
function check_str($string, $isurl = false)
{
$string = preg_replace('/[//x00-//x08//x0B//x0C//x0E-//x1F]/','',$string);
$string = str_replace(array("/0","%00","/r"),'',$string);
empty($isurl) && $string = preg_replace("/&(?!(#[0-9]+|[a-z]+);)/si",'&',$string);
$string = str_replace(array("%3C",'<'),'<',$string);
$string = str_replace(array("%3E",'>'),'>',$string);
$string = str_replace(array('"',"'","/t",' '),array('“','‘',' ',' '),$string);
return trim($string);
}


下面是为大家整理的一些过滤函数:

代码如下:
/**
* 安全过滤类-过滤javascript,css,iframes,object等不安全参数 过滤级别高
*  Controller中使用方法:$this->controller->fliter_script($value)
* @param  string $value 需要过滤的值
* @return string
*/
function fliter_script($value) {
$value = preg_replace("/(javascript:)?on(click|load|key|mouse|error|abort|move|unload|change|dblclick|move|reset|resize|submit)/i","&111n//2",$value);
$value = preg_replace("/(.*?)<//script>/si","",$value);
$value = preg_replace("/(.*?)<//iframe>/si","",$value);
$value = preg_replace ("//iesU", '', $value);
return $value;
}

/**
* 安全过滤类-过滤HTML标签
*  Controller中使用方法:$this->controller->fliter_html($value)
* @param  string $value 需要过滤的值
* @return string
*/
function fliter_html($value) {
if (function_exists('htmlspecialchars')) return htmlspecialchars($value);
return str_replace(array("&", '"', "'", "<", ">"), array("&", "/"", "'", "<", ">"), $value);
}

/**
* 安全过滤类-对进入的数据加下划线 防止SQL注入
*  Controller中使用方法:$this->controller->fliter_sql($value)
* @param  string $value 需要过滤的值
* @return string
*/
function fliter_sql($value) {
$sql = array("select", 'insert', "update", "delete", "/'", "///*",
     "/././/", "/.//", "union", "into", "load_file", "outfile");
$sql_re = array("","","","","","","","","","","","");
return str_replace($sql, $sql_re, $value);
}

/**
* 安全过滤类-通用数据过滤
*  Controller中使用方法:$this->controller->fliter_escape($value)
* @param string $value 需要过滤的变量
* @return string|array
*/
function fliter_escape($value) {
if (is_array($value)) {
  foreach ($value as $k => $v) {
   $value[$k] = self::fliter_str($v);
  }
} else {
  $value = self::fliter_str($value);
}
return $value;
}

/**
* 安全过滤类-字符串过滤 过滤特殊有危害字符
*  Controller中使用方法:$this->controller->fliter_str($value)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表