Black and white list: PHP implementation IP Black and white list filter

Source: Internet
Author: User
Tags filter foreach array exit php file

Requirements from a working colleague, the implementation of a PHP file IP filtering, not convenient to configure the server directly, so need to directly at the beginning of the php file for IP filtering.
IP filtering rules can have the following forms:
1. Complete IP address such as: 192.168.0.1
2. A section of IP such as: 192.168.0.*.
Operation dimension can be customized IP black and white list, composed of multiple IP filtering rules, stored in the array. By writing code, the IP black and white list function is implemented. A relatively simple requirement.
The first is to implement a function that determines whether IP is compliant with an IP filter rule:

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;
}
Implementation of the core function of Ip_test, the following process is much simpler, nothing more than a simple traversal of each rule in the list, to determine whether the current connected IP compliance with the rules, and the corresponding steps.
Whitelist, continuing the operation when IP satisfies at least one rule

$curr _ip=$_server[' remote_addr '];
$white _list=array (...); White list 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, continue operation when IP does not meet 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 ');
}
}
In this way, a simple PHP implementation of the IP black and white list filtering is done. This blog is mainly for those who are not the main business development personnel, such as: technical support, operations and so on.   Because too simple, originally did not want to write, then the colleague thanked me for help, told me that he looked for a long time on the internet, did not find the right solution, I think maybe this is really someone need it. This article links http://www.cxybl.com/html/wlbc/Php/20130326/37394.html

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.