How to filter IP blacklist and whitelist using php
- 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;
- }
After the core function ip_test is implemented, the following process is much simpler. it is nothing more than simply traversing each rule in the list to determine whether the IP address of the current connection meets the rule, and perform the corresponding steps. Whitelist. when the IP address must meet at least one rule, perform the following operations:
- $ Curr_ip = $ _ SERVER ['remote _ ADDR '];
- $ White_list = array (...); // whitelist rule
- $ Test_success = false;
- Foreach ($ white_list as $ iprule ){
- If (ip_test ($ curr_ip, $ iprule )){
- $ Test_success = true;
- Break;
- }
- }
- If (! $ Test_success) exit ('IP not in white list ');
Blacklist. if the IP address does not meet all the rules, continue the operation:
- $ Curr_ip = $ _ SERVER ['remote _ ADDR '];
- $ Black_list = array (...); // blacklist rule
- Foreach ($ black_list as $ iprule ){
- If (ip_test ($ curr_ip, $ iprule )){
- Exit ('IP in black list ');
- }
- }
So far, a simple php ip blacklist and whitelist filtering is complete, hoping to help you. Articles you may be interested in: PHP Security filter code (360 high security factor) PHP filter post, get sensitive data instance code php filter illegal and special string method php anti-injection of a piece of code (filter parameters) very useful php prevent SQL injection vulnerability filter function code php prevent SQL injection regular filter a piece of php filter dangerous html code |