php geo ip loc不起作用 [英] php geo ip loc does not work

查看:390
本文介绍了php geo ip loc不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

您好,我正在尝试设置它,但每当我使用 include(geoiploc.php); 时,页面都是空白的,每当我删除 include(geoiploc.php); 我只看到我的IP地址。我已经将geoiploc.php和index.php上传到可以运行PHP的webhost。

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.

如果有其他更简单的方式通过IP或任何其他方式显示国家/地区名称,这是什么?我需要这么快

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

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

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

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

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


推荐答案

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

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

然后从这里下载地理国家数据库: http://dev.maxmind.com/geoip/legacy/geolite

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

?>

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

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 . "\n";
    geoip_close($gi);

?>

这篇关于php geo ip loc不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆