zoukankan      html  css  js  c++  java
  • php代码判断地区省市或者ip段

    <?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';";   
    }
    
    ?>
  • 相关阅读:
    团队第三次作业-王者终篇
    第二次团队任务之个人
    团队作业总和
    本次分工
    【软件工程】第一次作业
    面向对象第三次作业(向计算器进发)
    面向对象程序设计第二次作业(1)
    面向对象程序设计第二次作业(2)
    我的大一上(福州)
    第一周例行报告
  • 原文地址:https://www.cnblogs.com/linyusong/p/7400520.html
Copyright © 2011-2022 走看看