Content filtering is mainly to prevent some security injections or cross-domain operations. Let’s take a look at some simple anti-injection content filtering program codes that I compiled. I hope the article will be helpful to all students.
Method 1, filter some useless content
The filtering of useless information is strict, and useful information may not be entered. You can find other filtering methods on the Internet:
The code is as follows
代码如下
复制代码
function checkHtml($data){
$ret = preg_match("/['.,:;*?~`!@#$%^&+=)(<>{}]|]|[|/||"||/",$data);
if ($ret == 1) {
return false; exit;
} else {
return true;
}
}
function uh($str)
{
$farr = array(
"/s+/", //Filter excess whitespace
"/<(/?)(scripti?framestylehtmlbodytitlelinkmeta?%)([^>]*?)>/isU", //Filter
"/(<[^>]*)on[a-zA-Z]+s*=([^>]*>)/isU", //Filter the on event of javascript
);
$tarr = array(
" ",
"<123>", //If you want to directly clear unsafe tags, you can leave it blank here
"12",
);
$str = preg_replace( $farr,$tarr,$str);
return $str;
}
Method three, the above two methods both put the content to be filtered in the program. Next, I put the content to be filtered into a txt text. The second time, I only need to read the file content to make a judgment, which is convenient for maintenance. Content to filter.
The code is as follows
Copy code
if($_POST)
{
//Two ways to get the file content and convert it into an array:
/*
$fcon = file_get_contents("./filter.txt");
$filter_word = explode("n",$fcon);
*/
$filter_word = file("./filter.txt");
//$filter_word = array("test1","test2","test3","test4");
$str = $_POST["mess"];
for($i=0;$i
{
if(preg_match("/".(trim($filter_word[$i]))."/i",$str))
{
echo "<script>alert('The content you entered contains illegal content, please re-enter!');</script>";
echo "return";
exit;
}
}
echo "The content you entered is:".$str;
}
?>
Test whether filtering is effective:
http://www.bkjia.com/PHPjc/632820.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632820.htmlTechArticleContent filtering is mostly to prevent some security injections or cross-domain operations. Let’s take a look at what I have compiled A few simple anti-injection content filtering program codes, I hope the article will be helpful to everyone...
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