- function Ip_test ($ip, $iprule) {
- $ipruleregexp =str_replace ('. * ', ' ph ', $iprule);
- $ipruleregexp =preg_quote ($ipruleregexp, '/');
- $ipruleregexp =str_replace (' ph ', ' \.[ 0-9]{1,3} ', $ipruleregexp);
- if (Preg_match ('/^ '. $ipruleregexp. ' $/', $ip)) return true;
- else return false;
- }
Copy CodeAfter implementing the core function of ip_test, the following procedure is much simpler, simply traversing each rule in the list, judging whether the IP currently connected is compliant with the rules, and doing the corresponding steps. Whitelist to continue the operation when the IP satisfies at least one rule:
- $curr _ip=$_server[' remote_addr '];
- $white _list=array (...); White List Rules
- $test _success=false;
- foreach ($white _list as $iprule) {
- if (Ip_test ($curr _ip, $iprule)) {
- $test _success=true;
- Break
- }
- }
- if (! $test _success) exit (' IP not at White List ');
Copy CodeBlacklist, continue operation when IP does not satisfy all rules:
- $curr _ip=$_server[' remote_addr '];
- $black _list=array (...); Blacklist rules
- foreach ($black _list as $iprule) {
- if (Ip_test ($curr _ip, $iprule)) {
- Exit (' IP in black List ');
- }
- }
Copy CodeAt this point, a simple PHP implementation of the IP black-and-white list filter is complete, hoping to bring you help. Articles you may be interested in: PHP Security Filter Code (360 provides high safety factor) PHP filter Post,get sensitive data Instance code PHP filter illegal with special string method PHP anti-injection a section of code (filter parameters) Very good PHP code to prevent SQL Injection Vulnerability Filter function PHP prevent SQL injection Regular filtering a case of PHP filtering dangerous HTML code |