php中ip获取与处理

Posted by
ip转化为整数:
ip2long();
转化回来long2ip();
百度IP接口api:

/*
新浪IP接口
array (size=10)
‘ret’ => int 1
‘start’ => string ‘106.120.0.0’ (length=11)
‘end’ => string ‘106.121.255.255’ (length=15)
‘country’ => string ‘中国’ (length=6)
‘province’ => string ‘北京’ (length=6)
‘city’ => string ‘北京’ (length=6)
‘district’ => string ” (length=0)
‘isp’ => string ‘电信’ (length=6)
‘type’ => string ” (length=0)
‘desc’ => string ” (length=0)
*/
function getIpPlace(){
$json_ip = file_get_contents(‘http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json’);
$array_ip = json_decode($json_ip,true);
return $array_ip;
}

//获取本地IP
function getIP()
{
static $realip;
if (isset($_SERVER)){
if (isset($_SERVER[“HTTP_X_FORWARDED_FOR”])){
$realip = $_SERVER[“HTTP_X_FORWARDED_FOR”];
} else if (isset($_SERVER[“HTTP_CLIENT_IP”])) {
$realip = $_SERVER[“HTTP_CLIENT_IP”];
} else {
$realip = $_SERVER[“REMOTE_ADDR”];
}
} else {
if (getenv(“HTTP_X_FORWARDED_FOR”)){
$realip = getenv(“HTTP_X_FORWARDED_FOR”);
} else if (getenv(“HTTP_CLIENT_IP”)) {
$realip = getenv(“HTTP_CLIENT_IP”);
} else {
$realip = getenv(“REMOTE_ADDR”);
}
}
return $realip;
}

/*
淘宝IP接口
array (size=13)
‘country’ => string ‘未分配或者内网IP’ (length=23)
‘country_id’ => string ‘IANA’ (length=4)
‘area’ => string ” (length=0)
‘area_id’ => string ” (length=0)
‘region’ => string ” (length=0)
‘region_id’ => string ” (length=0)
‘city’ => string ” (length=0)
‘city_id’ => string ” (length=0)
‘county’ => string ” (length=0)
‘county_id’ => string ” (length=0)
‘isp’ => string ” (length=0)
‘isp_id’ => string ” (length=0)
‘ip’ => string ‘127.0.0.1’ (length=9)
*/

function getCity($ip)
{
$url=”http://ip.taobao.com/service/getIpInfo.php?ip=”.$ip;
$ip=json_decode(file_get_contents($url));
if((string)$ip->code==’1′){
return false;
}
$data = (array)$ip->data;
return $data;
}

//取客户端 ip
function get_client_ip()
{
    if (isset($_SERVER)){
            if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
                $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
            } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
                $realip = $_SERVER["HTTP_CLIENT_IP"];
            } else {
                $realip = $_SERVER["REMOTE_ADDR"];
            }
    } else {
            if (getenv("HTTP_X_FORWARDED_FOR")){
                $realip = getenv("HTTP_X_FORWARDED_FOR");
            } else if (getenv("HTTP_CLIENT_IP")) {
                $realip = getenv("HTTP_CLIENT_IP");
            } else {
                $realip = getenv("REMOTE_ADDR");
            }
        }
    return $realip;
}

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据