这是我获取国家以及访问者的 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;
}
}
}
}
}
希望它对某人有所帮助!