Home  >  Article  >  Backend Development  >  PHP determines whether there is a malicious character instance_PHP tutorial

PHP determines whether there is a malicious character instance_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:47:421115browse

The principle of using PHP to filter malicious characters is very simple. We only need to define the characters and then use foreach to traverse the malicious character library, and use strpost to detect whether there are any characters in the malicious character library in the data submitted by the user. Then we can instantiate it.

Example 1, determine whether a character exists

The code is as follows
 代码如下 复制代码

function is_bad_chars($str){
 $bad_chars = array("",' ',"'",'"','/','*',',','<','>',"r","t","n",'$','(',')','%','+','?',';','^','#',':',' ','`','=','|','-');
 foreach($bad_chars as $value){
  if (strpos($str,$value) !== false){
   return true;
  }
 }
}

Copy code

 代码如下 复制代码

function outip($ip){//排除ip
@$txt = file_get_contents("ip/ip.txt" );
$txtarr = explode("rn" , trim($txt));
 if(count($txtarr)<0){
  return false;
    }
   if(in_array($ip,$txtarr)){
  return true;
 }else{
  return false;
 }
}

function is_bad_chars($str){
$bad_chars = array("",' ',"'",'"','/','*',',','<','>',"r","t"," n",'$','(',')','%','+','?',';','^','#',':',' ','`', '=','|','-');
foreach($bad_chars as $value){
if (strpos($str,$value) !== false){
Return true;
}
}
}

Based on the above example we can make an example of filtering keywords
The code is as follows

Copy code
function outip($ip){//exclude ip
@$txt = file_get_contents("ip/ip.txt" );
$txtarr = explode("rn" , trim($txt));
if(count($txtarr)<0){
return false;
}
if(in_array($ip,$txtarr)){
return true;
}else{
return false;
}
} Put some words you want to filter in the ip.txt file. Each word or phrase is one line http://www.bkjia.com/PHPjc/632830.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632830.htmlTechArticleThe principle of using php to filter malicious characters is very simple. We only need to define the characters and then use foreach to traverse the malicious character library and use strpost Detect whether there are malicious characters in the data submitted by the user...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn