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> |