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.