【问题标题】:GEOIP and getting a computers IP address Location?GEOIP 并获取计算机 IP 地址 位置?
【发布时间】:2012-10-21 02:52:21
【问题描述】:

我希望我的网站能够定位计算机位置,因此如果有人从伦敦或曼彻斯特等地访问我的网站,并根据他们的计算机位置显示来自某个区域的用户。有点像一个在线约会网站,向您所在地区的用户推荐。

我一直在查看这个列出全球所有城市的 GEOIP 数据库。但我不知道下一步该怎么做?我是否需要查看从 GEOip 数据库中提取和比较信息的获取 IP 地址脚本?

请有人指出我正确的方向。谢谢。

GeoIP 数据库来自: http://dev.maxmind.com/geoip/geolite

【问题讨论】:

    标签: php ip


    【解决方案1】:

    试试下面的代码。

    $ip =  $_SERVER['REMOTE_ADDR'];
    echo $location = file_get_contents("http://api.hostip.info/country.php?ip=$ip");
    

    不要在你的本地主机上尝试这个。它将提供 ip 127.0.0.1。

    echo $location= file_get_contents("http://api.hostip.info/country.php?ip=12.215.42.19");
    //outputs US
    

    【讨论】:

      【解决方案2】:

      PHP 具有 GeoIP 的内置函数

      GeoIP library

      这个函数可能特别有用,因为它会以数组的形式返回大陆、国家、城市等。

      geoip_record_by_name

      使用$_SERVER['REMOTE_ADDR'] 作为您的 ip。

      $record = geoip_record_by_name( $_SERVER['REMOTE_ADDR']);

      【讨论】:

        【解决方案3】:

        这取决于您的需要。我建议您使用 Web 服务,这样您就不必担心托管数据库并自行维护它。您需要做的就是解析访问者的 IP 地址并立即获得结果。我正在使用来自this site 的 IP2location 网络服务。

        【讨论】:

        • 嗨 HexaHow - 我在您的帖子中更改了一些内容。您是否已经找到了格式化答案的帮助(例如添加链接)?
        【解决方案4】:

        我在我的网站上的 PHP 中使用了这个。非常快速且易于消化(xml 等):http://www.geoplugin.com/

        【讨论】:

          【解决方案5】:

          请注意,这个数据库是:In our recent tests, the GeoIP databases tested at 99.8% accurate on a country level, 90% accurate on a state level in the US, and 83% accurate for cities in the US within a 40 kilometer radius.

          我会研究类似的东西:PEAR GeoLite 库

          在php中使用这个库的快速sn-p:

          $geoip = Net_GeoIP::getInstance(dirname(__FILE__) . '/data/GeoLiteCity.dat');
          $ipaddress = '72.30.2.43'; // Yahoo!
          $location = $geoip->lookupLocation($ipaddress);
          var_dump($location);
          

          输出将显示,类似于:

          object(Net_GeoIP_Location)[2]
              protected 'aData' =>
                  array
                  'countryCode' => string 'US' (length=2)
                  'countryCode3' => string 'USA' (length=3)
                  'countryName' => string 'United States' (length=13)
                  'region' => string 'CA' (length=2)
                  'city' => string 'Sunnyvale' (length=9)
                  'postalCode' => string '94089' (length=5)
                  'latitude' => float 37.4249
                  'longitude' => float -122.0074
                  'areaCode' => int 408
                  'dmaCode' => float 807
          

          【讨论】:

            【解决方案6】:

            您可能已经将GEOIP 作为PECL 扩展安装。如果你不这样做,那么你可以自己安装它。

            如果您需要比国家/地区更具体的内容,则必须付费才能获得数据库,但功能仍然存在。

            【讨论】:

              【解决方案7】:

              您可以使用https://geoip-db.com的服务。返回给定 IP 地址位置的 php JSONP 示例。

              <?php
              
                  $json = file_get_contents('https://geoip-db.com/jsonp/82.196.6.158');
                  $data = jsonp_decode($json);
              
                  print $data->country_code . '<br>';
                  print $data->country_name . '<br>';
                  print $data->state . '<br>';
                  print $data->city . '<br>';
                  print $data->postal . '<br>';
                  print $data->latitude . '<br>';
                  print $data->longitude . '<br>';
                  print $data->IPv4 . '<br>';
              
                  function jsonp_decode($jsonp, $assoc = false) {
                      if($jsonp[0] !== '[' && $jsonp[0] !== '{') {
                          $jsonp = substr($jsonp, strpos($jsonp, '('));
                      }
                      return json_decode(trim($jsonp,'();'), $assoc);
                  }
              ?>
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2019-04-26
                • 2012-09-01
                • 2010-09-12
                • 2011-06-14
                • 1970-01-01
                • 2011-09-11
                • 2014-04-15
                • 1970-01-01
                相关资源
                最近更新 更多