PHP

首页 -  PHP  -  php根据ip地址获取城市-得到客户端IP

php根据ip地址获取城市-得到客户端IP

php根据ip地址获取城市

/**
 * @return string
 * 得到客户端IP
 */
function clientIP()
{
    $cIP = getenv('REMOTE_ADDR');
    $cIP1 = getenv('HTTP_X_FORWARDED_FOR');
    $cIP2 = getenv('HTTP_CLIENT_IP');
    $cIP1 ? $cIP = $cIP1 : null;
    $cIP2 ? $cIP = $cIP2 : null;
    return $cIP;
}

/**
 * @return string
 * 获取城市
 */
function ipToCity()
{
    $ip = clientIP();
    $cityDate = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip);
    $array = json_decode($cityDate);
    return $array;
}


(0)
分享:

本文由:xiaoshu168 作者:xiaoshu发表,转载请注明来源!

标签:

相关阅读