• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

php代码判断地区省市或者ip段

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<?php
function getClientIp($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'];
    }
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}
//获取省
function getIpLocal($ip){
    if($ip==""||$ip==null)
        $ip=getClientIp();
    $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=".$ip;
    $json = json_decode(file_get_contents($url));
    $city=$json->city;    
    $city=str_replace('市','',$city);
    $city = iconv("utf-8","gb2312",$city);//字符串转码
    $province=$json->province;
    $province=str_replace('省','',$province);
    $province = iconv("utf-8","gb2312",$province);//字符串转码
    return $province;
    
}
//获取市
function getIpLocal_city($ip){
    if($ip==""||$ip==null)
        $ip=getClientIp();
    $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=".$ip;
    $json = json_decode(file_get_contents($url));
    $city=$json->city;    
    $city=str_replace('市','',$city);
    $city = iconv("utf-8","gb2312",$city);//字符串转码
    $province=$json->province;
    $province=str_replace('省','',$province);
    $province = iconv("utf-8","gb2312",$province);//字符串转码
    return $city;
    
}


/*$diqu=getIpLocal("14.215.177.37");*/


//获取省
$diqu=getIpLocal(getClientIp());
//获取市
$diqu_city=getIpLocal_city(getClientIp());


//判断省
if($diqu=='北京'||$diqu=='江苏'){
    echo "window.location.href='http://www.baidu.com';";
}
//判断市
if($diqu_city=='北京'){
    echo "window.location.href='http://www.baidu.com';";
}

//判断ip段
if($ip==""||$ip==null){
        $ip=getClientIp();
        }     
$arrayip = array('180.76.14.*','180.97.79.*','60.194.2.*');//ip段  
$ipregexp = implode('|', str_replace( array('*','.'), array('\d+','\.') ,$arrayip) );  

if(preg_match("/^(".$ipregexp.")$/", $ip)==1){
    
    echo "window.location.href='http://www.baidu.com';";
    
}

?>

 由于http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪接口目前用不了 ,下面用淘宝提供的接口

<?php
function getClientIp($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'];
    }
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}
//获取省
function getIpLocal($ip){    
    if($ip==""||$ip==null)
    $ip=getClientIp();
    $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
    $content =file_get_contents($url);
    $arr = json_decode($content,true);
    return $arr["data"]["region"]; 
}
//获取市
function getIpLocal_city($ip){    
    if($ip==""||$ip==null)
    $ip=getClientIp();
    $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
    $content =file_get_contents($url);
    $arr = json_decode($content,true);
    return $arr["data"]["city"];   
}
/*$diqu=getIpLocal("14.215.177.37");*/
//获取省
$diqu=getIpLocal(getClientIp());
//获取市
$diqu_city=getIpLocal_city(getClientIp());
//判断省
if($diqu=='北京'|| $diqu=='福建'){    
    echo "window.location.href='http://www.baidu.com';";    
}
//判断市    
if($diqu_city=='北京' || $diqu_city=='上海' || $diqu_city=='广州'|| $diqu_city=='深圳' || $diqu_city=='杭州'){
    echo "window.location.href='http://www.baidu.com';";    
}
//判断ip段
if($ip==""||$ip==null){
    $ip=getClientIp();
}     
$arrayip = array('180.76.14.*','180.97.79.*','60.194.2.*');//ip段  
$ipregexp = implode('|', str_replace( array('*','.'), array('\d+','\.') ,$arrayip) );  
if(preg_match("/^(".$ipregexp.")$/", $ip)==1){    
    echo "window.location.href='http://www.baidu.com';";   
}

?>

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php实现7种常见排序发布时间:2022-07-10
下一篇:
mac下编译php的openssl发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap