【问题标题】:PHP - Detect country [duplicate]PHP - 检测国家[重复]
【发布时间】:2013-01-03 02:16:43
【问题描述】:

可能重复:
package for detecting users contry in php?
Geo Location based on IP Address - PHP

我想检测访问者所在的国家/地区。

我在 Google 找到了解决方案, 但结果什么都没有。

你能帮帮我吗?

【问题讨论】:

  • 我非常怀疑 Google 没有返回任何有用的信息。
  • 当然,但我找不到我想要的
  • 那么,你喜欢哪个答案?
  • 猜你是在谷歌搜索“帮我谷歌,我不知道如何在 PHP 中检测国家,请帮助我,我不知道如何使用谷歌”

标签: php geo country country-codes


【解决方案1】:

这是我获取国家以及访问者的 IP 的方式。

// this is where you get the ip
$ip = $_SERVER['REMOTE_ADDR'];

// this is where you include the code that gets the country
// you can find the code for this file on the link below    
include("geoiploc.php");

// this is where you create the variable that get you the name of the country
$country = getCountryFromIP($ip, " NamE ");

php geo ip loc does not work

希望这会有所帮助。

愿代码与你同在!

更新:

这是我一直在使用的另一种方法

    $json = file_get_contents('http://ipinfo.io/' . $this->getIP());
    $data = json_decode($json);
    return $data->country;

也有这个服务,不过我发现上面那个好多了……

    'http://getcitydetails.geobytes.com/GetCityDetails?fqcn=' . $this->getIP()

这是获取ip的好方法:

private function getIP() {
    $server_keys = [
        'HTTP_CLIENT_IP',
        'HTTP_X_FORWARDED_FOR',
        'HTTP_X_FORWARDED',
        'HTTP_X_CLUSTER_CLIENT_IP',
        'HTTP_FORWARDED_FOR',
        'HTTP_FORWARDED',
        'REMOTE_ADDR'
    ];

    foreach ($server_keys as $key) {
        if (array_key_exists($key, $_SERVER) === true) {
            foreach (explode(',', $_SERVER[$key]) as $ip) {
                if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                    return $ip;
                }
            }
        }
    }
}

希望它对某人有所帮助!

【讨论】:

  • 这里是 geoiploc.php 文件的完整文档:chir.ag/projects/geoiploc
  • 谢谢Daniel,如果你使用geoiploc ...你是不是很慢?
  • 我确实使用过它,我用它来记录我的网站的访问量,并且很高兴存储国家/地区。如果我慢慢来,你是什么意思?
【解决方案2】:
    <?php 
// Author: www.easyjquery.com 
$ip = $_SERVER['REMOTE_ADDR']; 
// remember chmod 0777 for folder 'cache' 
$file = "./cache/".$ip; 
if(!file_exists($file)) { 
    // request 
    $json = file_get_contents("http://api.easyjquery.com/ips/?ip=".$ip."&full=true"); 
    $f = fopen($file,"w+"); 
    fwrite($f,$json); 
    fclose($f); 
} else { 
    $json = file_get_contents($file); 
} 

$json = json_decode($json,true); 
echo "<pre>"; 
print_r($json); 

?>

Demo Link

【讨论】:

  • 凯利你能编辑你的答案并添加一些本地解决方案吗,我不想使用外部解决方案,如果你能做到,我会接受你
  • 嗨迈克尔,在这里查看,该信息也可用于下载 js 文件,以便您自己托管解决方案,希望对您有所帮助:) easyjquery.com/2012/11/…
  • type 2 -- fopen(./cache/72.193.132.49): failed to open stream: No such file or directory -- at line 9
猜你喜欢
  • 1970-01-01
  • 2014-03-11
  • 2013-04-19
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多