Source: github.com/hacfins/thi…

Similar blog many, multifarious, to a function of their own packaging, directly on the code, very simple.

1. Plan 1

You only need to call the API interface of Taobao.com to obtain the geographical location information of the public network or LAN. ip.taobao.com/

/** * Obtain the address location by Ip */
function getIpInfo($internetIp = ' ')
{
    try
    {
        / / IP network
        // Class A 10.0.0.0 to 10.255.255.255
        Class B 172.16.0.0 to 172.31.255.255
        // Class C 192.168.0.0 to 192.168.255.255
        / /...
        $bLocalIp = !filter_var($internetIp, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
        if($bLocalIp)
            $internetIp = 'myip';// LAN IP address
            
        $requestAPi = "http://ip.taobao.com/service/getIpInfo.php?ip=" . $internetIp;
        $opts       = array(
            'http'= >array(
                'method'= >'GET'.'timeout'= >1./ / unit of seconds));$jsonArr = json_decode( file_get_contents($requestAPi.false, stream_context_create($opts)),
            JSON_HEX_TAG | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_HEX_APOS );
            
        // Indicates that the network is disconnected
        if (!isset($jsonArr) || !isset($jsonArr['code']) {return false;
        }
        
        // 0 indicates success
        if ($jsonArr['code']! = =0)
        {
            return false;
        }
        
		// Return data result:
        / / "IP" : "223.98.166.115",
        // "country": "China ",
        // "area": "",
        // "region": "shandong ",
        // "city": "city",
        // "county": "XX",
        // "isp": "mobile ",
        // "country_id": "CN",
        // "area_id": "",
        // "region_id": "370000",
        // "city_id": "370100",
        // "county_id": "xx",
        // "isp_id": "100025"
        $data = (array)$jsonArr['data'];
        return $data;
    }
    catch (\Exception $e) {}return false;
}
Copy the code

Taobao API has a limited call frequency

2. Plan 2

Ip-api.com/json/?lang=…

Ip-api.com/json/115.19…

3. Obtain the CLIENT IP address

/** * Obtain the client IP address *@paramInt $type [IP address type] *@paramBool $strict [whether to get in strict mode] *@returnMixed [client IP address] */
function client_ip($type = 0.$strict = false)
{
    $ip = null;
    // 0 returns the field address (127.0.0.1)
    // 1 return long integer address (2130706433)
    $type = $type ? 1 : 0;
    if ($strict) {
        /* Strict mode to prevent IP address masquerade */
        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(current($arr));
        } elseif (isset($_SERVER['HTTP_CLIENT_IP']) {$ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['REMOTE_ADDR']) {$ip = $_SERVER['REMOTE_ADDR']; }}else if (isset($_SERVER['REMOTE_ADDR']) {$ip = $_SERVER['REMOTE_ADDR'];
    }
    /* Verify IP address validity */
    $long = sprintf("%u", ip2long($ip));
    $ip = $long ? [$ip.$long] : ['0.0.0.0'.0];
    return $ip[$type];
}
Copy the code