0

http://chir.ag/projects/geoiploc/

您好,我正在尝试进行设置,但是每当我使用include("geoiploc.php");该页面时,页面都是空白的,并且每当我删除时,include("geoiploc.php");我只会看到我的 IP 地址。我已将 geoiploc.php 和 index.php 上传到可以运行 PHP 的虚拟主机。

如果有任何其他更简单的方法可以通过 IP 或任何其他方式显示国家名称,那是什么?请我需要这个快

所以正如我所说,它只在我删除include("geoiploc.php");为什么时显示我的IP?

你需要这个库:http ://chir.ag/projects/geoiploc/autogen/geoiploc.tar.gz这是运行脚本的代码:

include("geoiploc.php"); // Must include this

  // ip must be of the form "192.168.1.100"
  // you may load this from a database
  $ip = $_SERVER["REMOTE_ADDR"];
  echo "Your IP Address is: " . $ip . "<br />";

  echo "Your Country is: ";
  // returns country code by default
  echo getCountryFromIP($ip);
  echo "<br />\n";

  // optionally, you can specify the return type
  // type can be "code" (default), "abbr", "name"

  echo "Your Country Code is: ";
  echo getCountryFromIP($ip, "code");
  echo "<br />\n";

  // print country abbreviation - case insensitive
  echo "Your Country Abbreviation is: ";
  echo getCountryFromIP($ip, "AbBr");
  echo "<br />\n";

  // full name of country - spaces are trimmed
  echo "Your Country Name is: ";
  echo getCountryFromIP($ip, " NamE ");
  echo "<br />\n";
4

1 回答 1

3

您可以使用免费的 MaxMind Geo Lite。在此处下载文件:http: //www.maxmind.com/download/geoip/api/php/php-latest.tar.gz

然后从这里下载 Geo Country 数据库:http: //dev.maxmind.com/geoip/legacy/geolite

您现在可以像这样使用它:

<?php

    include("geoip.inc");

    function ipAddress(){
        if (!empty($_SERVER['HTTP_CLIENT_IP'])){ //check ip from share internet
            $ip=$_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ // proxy pass ip
            $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip=$_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    $gi = geoip_open("path/to/GeoIP.dat",GEOIP_STANDARD);

    echo geoip_country_name_by_addr($gi, ipAddress());
    // echo geoip_country_code_by_addr($gi, ipAddress()); <-- country code   

    geoip_close($gi);

?>

更新:要获取用户所在的城市,您应该下载顶部链接并查找调用的文件sample_city.php以查看一些示例代码。您需要下载此文件: http: //geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz并将其放在与您的 php 文件相同的目录中。一个简单的例子是:

<?php

    include("geoipcity.inc");
    function ipAddress(){
        if (!empty($_SERVER['HTTP_CLIENT_IP'])){ //check ip from share internet
            $ip=$_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ // proxy pass ip
            $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip=$_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
    $gi = geoip_open("GeoIPCity.dat",GEOIP_STANDARD);
    $record = geoip_record_by_addr($gi,ipAddress());
    print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "\n";
    geoip_close($gi);

?>
于 2013-05-22T22:03:42.077 回答