PHP implementation of IP White list blacklist function _php skills

Source: Internet
Author: User
Tags get ip

This is a detection of IP is illegal PHP function, to adapt to the whitelist, blacklist function development, the main scenario applied: API source restrictions, access restrictions and so on.

Copy Code code as follows:

/**
* Secure IP detection, support IP segment detection
* @param string $ip The IP to be detected
* @param string|array $ips white list IP or blacklist IP
* @return Boolean true is in whitelist or blacklist, otherwise not
*/
function is_safe_ip ($ip = "", $ips = "") {
if (! $ip) $ip = Get_client_ip (); Get Client IP
if ($ips) {
if (is_string ($ips)) {//IP with "," such as white list ip:192.168.1.13,123.23.23.44,193.134.*.*
$ips = Explode (",", $ips);
}
}else{//Read background configuration white list IP
$obj = new Setting ();
$ips = Explode (",", $obj->getconfig ("Whiteip"));
}
if (In_array ($ip, $ips)) {
return true;
}
$ipregexp = implode (' | '), str_replace (Array (' * ', '. '), Array (' \d+ ', '. '), $ips));
$rs = Preg_match ("/^ $ipregexp.") $/", $IP);
if ($RS) return true;
return;
}

Get IP address, here reference thinkphp built-in function

Copy Code code as follows:

The GET_CLIENT_IP () function should be posted on the request of netizens
/**
* Get client IP Address
* @param integer $type return type 0 return IP address 1 return IPV4 address number
* @param boolean $ADV for Advanced mode acquisition (possibly disguised)
* @return Mixed
*/
function get_client_ip ($type = 0, $adv =false) {
$type = $type? 1:0;
static $IP = NULL;
if ($ip!== NULL) return $ip [$type];
if ($ADV) {
if (Isset ($_server[' http_x_forwarded_for ')) {
$arr = Explode (', ', $_server[' http_x_forwarded_for '));
$pos = Array_search (' unknown ', $arr);
if (false!== $pos) unset ($arr [$pos]);
$ip = Trim ($arr [0]);
}elseif (Isset ($_server[' http_client_ip ')) {
$ip = $_server[' http_client_ip '];
}elseif (Isset ($_server[' remote_addr ')) {
$ip = $_server[' remote_addr '];
}
}elseif (Isset ($_server[' remote_addr ')) {
$ip = $_server[' remote_addr '];
}
IP Address legal authentication
$long = sprintf ("%u", Ip2long ($IP));
$ip = $long? Array ($ip, $long): Array (' 0.0.0.0 ', 0);
return $ip [$type];
}

The above is the entire content of this article, I hope that you understand the PHP detection of IP help.

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.