doumangzhen7204 2013-05-22 21:49
浏览 31
已采纳

php geo ip loc不起作用

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

Hello, I am trying to set this up, but whenever I use include("geoiploc.php"); the page is blank and whenever I remove include("geoiploc.php"); I only see my IP address. I've uploaded geoiploc.php and index.php to a webhost that can run PHP.

If there is ANY other easier way to show country name by IP or any other way, which is it? Please I need this fast

So as I said, it only shows my IP whenever I remove include("geoiploc.php"); why?

You need this library: http://chir.ag/projects/geoiploc/autogen/geoiploc.tar.gz and this is the code to run the script:

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 />
";

  // 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 />
";

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

  // full name of country - spaces are trimmed
  echo "Your Country Name is: ";
  echo getCountryFromIP($ip, " NamE ");
  echo "<br />
";
  • 写回答

1条回答 默认 最新

  • dpcj40970 2013-05-22 22:03
    关注

    You can use the free MaxMind Geo Lite. Download the files here: http://www.maxmind.com/download/geoip/api/php/php-latest.tar.gz

    Then download the Geo Country database from here: http://dev.maxmind.com/geoip/legacy/geolite

    You can now use it like this:

    <?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);
    
    ?>
    

    UPDATE: to get the user's city you should download the top link and look for the file called sample_city.php to see some example code. You'll need to download this file: http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz and place it in the same directory as your php file. A quick example would be:

    <?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 . "
    ";
        geoip_close($gi);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?