Simple PHP anti-injection content filtering method

Source: Internet
Author: User
Tags html tags

Method One, filter some useless content

Filtering useless information is more rigorous, useful may also input not to go in, can look for other filtering methods on the Internet:

The code is as follows Copy Code

function checkhtml ($data) {
$ret = Preg_match ("/['.,:; *?~ '!@#$%^&+=)" (<>{}]|]| [|/|\|"|| /", $data);
if ($ret = = 1) {
return false; Exit
} else {
return true;
}
}

Example 2 filters some HTML tags.

The code is as follows Copy Code


Function uh ($STR)
{
$farr = Array (
"/s+/",//filter Excess blank
"/< (/?) (scripti?framestylehtmlbodytitlelinkmeta?%) ([^>]*?) >/isu ",//filter <script, etc. may introduce malicious content or malicious changes to display the layout of the code, if you do not need to insert flash, etc., you can also add <object filter
"/(<[^>]*) on[a-za-z]+s*= ([^>]*>)/isu",//filter JavaScript on event
);
$tarr = Array (
" ",
' <\1\2\3> ',//If you want to clear the unsafe label directly, leave this blank
"\1\2",
);
$str = Preg_replace ($farr, $tarr, $STR);
return $str;
}

Method Three, the above two methods are to filter content in the program, I will filter the content to a txt text only, the first time to read the contents of the file can be judged, so as to facilitate the maintenance of the content to be filtered.

The code is as follows Copy Code

<?php
if ($_post)
{
Get the contents of a file into an array two ways:
/*
$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 <count ($filter _word); $i + +)
{
if (Preg_match ("/"). ( Trim ($filter _word[$i])). " /i ", $str))
{
echo "<script>alert (' your input contains illegal content, please lose again!") ');</script> ";
echo "<a href= ' index.php ' > Return </a>";
Exit
}
}
echo "What you entered is:". $str;
}
?>
<form action= "<?php echo $_server[' php_self ';?>" method= "POST" >
<p><textarea name= "mess" cols= "rows=" "4" ></textarea></p>
<p><input type= "Submit" Name= "sub" value= "Send" ></p>
</form>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.