Home > Article > Backend Development > Simple PHP anti-injection content filtering method_PHP tutorial
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
|
Copy code
|
||||||||
$ret = preg_match("/['.,:;*?~`!@#$%^&+=)(<>{}]|]|[|/||"||/",$ data); if ($ret == 1) { Return false; exit; } else { Return true; } }
|
The code is as follows | Copy code | ||||
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.
|