澳门新葡萄京官网注册php下过滤HTML代码的函数

/*———————-
过滤HTML代码的函数
———————–*/
function htmlEncode($string) {
    $string=trim($string);
    $string=str_replace(“&”,”&”,$string);
    $string=str_replace(“‘”,”‘”,$string);
    $string=str_replace(“&”,”&”,$string);
    $string=str_replace(“"”,”””,$string);
    $string=str_replace(“””,”””,$string);
    $string=str_replace(“<“,”<“,$string);
    $string=str_replace(“<“,”<“,$string);
    $string=str_replace(“>”,”>”,$string);
    $string=str_replace(“>”,”>”,$string);
    $string=str_replace(” “,” “,$string);
    $string=nl2br($string);
    return $string;
}

//==防注入自动过滤[启用后程序效率低]==========================================================================================
/*
function inject_checks($sql_str){return
eregi(‘select|insert|update|delete|’|/*|*|../|./|union|into|load_file|outfile’,
$sql_str);}
foreach ($_REQUEST as $value){if (inject_checks($value)){echo
“<script
language=javascript>alert(‘你提交的数据非法,请检查后重新提交!’);</script>”;exit;}}
*/
//==防注[inject_check($sql_str)]==========================================================================================
function inject_check($sql_str){
if (eregi(‘select|insert|update|delete|union|into|load_file|outfile’,
$sql_str)){echo “<script
language=javascript>alert(‘你提交的数据非法,请检查后重新提交!’);</script>”;exit;}
return $sql_str;
}
//==字符过滤[safe_convert($string)]==============================================================================
function safe_convert($string){
//Words Filter
if(get_magic_quotes_gpc()){ //转义字符 加上反斜线
        $string=htmlspecialchars($string, ENT_QUOTES);
//将特殊字元转成HTML字串格式如 “&”转成”&”
        $string=str_replace(“<“,”<“,$string); //替换
        $string=str_replace(“>”,”>”,$string); //替换
        $string=str_replace(“”, ‘\’, $string); //替换  
    } else {
        $string=addslashes($string); //转义字符 加上反斜线
//$string=stripslashes($string); //去掉反斜线
        $string=str_replace(“\”, ‘\’, $string);
    }
    //$string=str_replace(“r”,”<br/>”,$string); //换行
    //$string=str_replace(“n”,””,$string); //空格
    $string=str_replace(“t”,”  “,$string); //空格
    $string=str_replace(”  “,”  “,$string); //空格
    //$string=str_replace(‘|’, ‘|’, $string); //替换
同分类系统有冲突
    $string=str_replace(“&#96;”,”`”,$string); //替换
    $string=str_replace(“&#92;”,”\”,$string); //替换
    return $string;
}
//==字符反过滤[unsafe_convert($string)]==============================================================================
function unsafe_convert($string){ //Words Filter
    $string=str_replace(“\"”,”"”,$string); //替换
    return $string;
}

//==字符过滤[filter($string)]============================================================================================
function filter($string){ //Words Filter
include(“Filter.php”);//词汇过滤列表
foreach($badwords as $badword){
if(stristr($string,$badword)==true){echo “<script
language=javascript>alert(‘警告:你提交的内容含有敏感字眼,请更换内容。’);</script>”;exit;}
}
return $string;
}

//安全过滤输入[jb]
function check_str($string, $isurl = false)
{
$string =
preg_replace(‘/[\x00-\x08\x0B\x0C\x0E-\x1F]/’,”,$string);
$string = str_replace(array(“”,”%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)
* @param  string $value 需要过滤的值
* @return string
*/
function fliter_str($value) {
$badstr = array(“”, “%00”, “r”, ‘&’, ‘ ‘, ‘”‘, “‘”, “<“, “>”,
”   “, “%3C”, “%3E”);
$newstr = array(”, ”, ”, ‘&’, ‘ ‘, ‘”‘, ”’, “<“, “>”, ”   “,
“<“, “>”);
$value  = str_replace($badstr, $newstr, $value);
$value  = preg_replace(‘/&((#(d{3,5}|x[a-fA-F0-9]{4}));)/’,
‘&\1’, $value);
return $value;
}

/**
* 私有路劲安全转化

Controller中使用方法:$this->controller->filter_dir($fileName)
* @param string $fileName
* @return string
*/
function filter_dir($fileName) {
$tmpname = strtolower($fileName);
$temp = array(‘:/’,””, “..”);
if (str_replace($temp, ”, $tmpname) !== $tmpname) {
  return false;
}
return $fileName;
}

/**
* 过滤目录
*  Controller中使用方法:$this->controller->filter_path($path)
* @param string $path
* @return array
*/
public function filter_path($path) {
$path = str_replace(array(“‘”,’#’,’=’,’`’,’$’,’%’,’&’,’;’), ”,
$path);
return rtrim(preg_replace(‘/(/){2,}|(\){1,}/’, ‘/’, $path),
‘/’);
}

/**
* 过滤PHP标签

Controller中使用方法:$this->controller->filter_phptag($string)
* @param string $string
* @return string
*/
public function filter_phptag($string) {
return str_replace(array(”), array(”), $string);
}

/**
* 安全过滤类-返回函数
*  Controller中使用方法:$this->controller->str_out($value)
* @param  string $value 需要过滤的值
* @return string
*/
public function str_out($value) {
$badstr = array(“<“, “>”, “%3C”, “%3E”);
$newstr = array(“<“, “>”, “<“, “>”);
$value  = str_replace($newstr, $badstr, $value);
return stripslashes($value); //下划线
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注