• PHP获取ip与ip所在城市


    1获取真实ip,本地测试总是::1 或者127.0.0.1 或者局域网的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;
    }
     
    复制代码

     2.根据ip获取地址

    复制代码
    /**
     * 获取 IP  地理位置
     * 淘宝IP接口
     * @Return: array
     */
    function getCity($ip = '')
    {
        if($ip == ''){
            $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
            $ip=json_decode(file_get_contents($url),true);
            $data = $ip;
        }else{
            $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;   
    }
    
    
    复制代码

    测试

    var_dump(getIP());
    var_dump(getCity());
    var_dump(getCity('218.93.250.162'));

    结果

    复制代码
    string(3) "::1"
    array(10) {
      ["ret"]=>
      int(1)
      ["start"]=>
      int(-1)
      ["end"]=>
      int(-1)
      ["country"]=>
      string(6) "中国"
      ["province"]=>
      string(6) "江苏"
      ["city"]=>
      string(6) "宿迁"
      ["district"]=>
      string(0) ""
      ["isp"]=>
      string(0) ""
      ["type"]=>
      string(0) ""
      ["desc"]=>
      string(0) ""
    }
    array(13) {
      ["country"]=>
      string(6) "中国"
      ["country_id"]=>
      string(2) "CN"
      ["area"]=>
      string(6) "华东"
      ["area_id"]=>
      string(6) "300000"
      ["region"]=>
      string(9) "江苏省"
      ["region_id"]=>
      string(6) "320000"
      ["city"]=>
      string(9) "宿迁市"
      ["city_id"]=>
      string(6) "321300"
      ["county"]=>
      string(0) ""
      ["county_id"]=>
      string(2) "-1"
      ["isp"]=>
      string(6) "电信"
      ["isp_id"]=>
      string(6) "100017"
      ["ip"]=>
      string(14) "218.93.250.162"
    }
    复制代码

    说明:新浪的接口,直接能获取到地址信息,淘宝的接口需要提供ip,不过获取的信息更全面

    转载:https://www.cnblogs.com/jiqing9006/p/4994728.html#commentform

  • 相关阅读:
    BootstrapValidator 解决多属性被同时校验问题《转》
    SSRS 浮动表头设置
    ToString(string format)输出格式简述
    配置AutoMapper映射规则《转》
    IE浏览器上传图片预览兼容(IE 7 8 9 10 11)
    SQL : IN 和 Exists 的区别
    BitArray简单例子
    Rx.net 例子——(1)基础
    C# Pinvoke判断是UEFI模式还是BIOS模式
    wpf Route Event Code Snippet
  • 原文地址:https://www.cnblogs.com/qq1069284034/p/8410624.html
Copyright © 2020-2023  润新知