PHP获取客户端IP,根据IP获取省份

获取客户端IP

/**
 * 获取登录Ip
 * @return mixed
 */
function getip()
{
    $ip = false;
    if(!empty( $_SERVER ["HTTP_CLIENT_IP"]))
    {
        $ip = $_SERVER ["HTTP_CLIENT_IP"];
    }
    if (!empty( $_SERVER ['HTTP_X_FORWARDED_FOR']))
    {
        $ips = explode ( ", ",$_SERVER ['HTTP_X_FORWARDED_FOR']);
        if ($ip)
        {
            array_unshift ($ips,$ip);
            $ip = FALSE;
        }
        for($i = 0; $i < count ( $ips ); $i ++)
        {
            if (!preg_match( "/^(10|172\.16|192\.168)\./",$ips[$i]))
            {
                $ip =$ips[$i];
                break;
            }
        }
    }
    $ip_str=($ip?$ip:$_SERVER['REMOTE_ADDR']);
    $ip_arr=explode(',',$ip_str);
    return $ip_arr[0];
}

根据IP获取省份

//获取省份
function getArea($ip)
{
    //$ip = getIp();
    $res1 = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip");
    //dp($res1);
    $res1 = json_decode($res1,true);


    if ($res1[ "code"]==0)
    {
        return $res1['data']["country"].$res1['data'][ "region"].$res1['data']["city"]."_".$res1['data'][ "isp"];
    }else{
        return "未知";
    }
}

你可能感兴趣的:(PHP获取客户端IP,根据IP获取省份)