我想知道PHP
使用我的网站的用户所在的国家/地区,所以我写这个函数来检测:
$details = json_decode(file_get_contents("http://ipinfo.io/"));
我尝试使用其他链接
$details = json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip={$ip}"));
$details = json_decode(file_get_contents("http://api.hostip.info/country.php?ip=$ip"));
我将这些用于2个站点,一个是暂存,一个是实时(不同的服务器)
在演出时它完美无缺。但在现场它总是返回虚假。为什么会这样?如何在我的实际网站上获取用户的国家/地区?
谢谢> _<
答案 0 :(得分:1)
试试这段代码:
where user_id = 35 instead of where username = 'dodlo.rg'
}
http://php.net/manual/en/function.geoip-country-name-by-name.php
答案 1 :(得分:0)
尝试另一种方式:
<?php
function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
$output = NULL;
if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
$ip = $_SERVER["REMOTE_ADDR"];
if ($deep_detect) {
if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
}
$purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
$support = array("country", "countrycode", "state", "region", "city", "location", "address");
$continents = array(
"AF" => "Africa",
"AN" => "Antarctica",
"AS" => "Asia",
"EU" => "Europe",
"OC" => "Australia (Oceania)",
"NA" => "North America",
"SA" => "South America"
);
if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
$ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
switch ($purpose) {
case "location":
$output = array(
"city" => @$ipdat->geoplugin_city,
"state" => @$ipdat->geoplugin_regionName,
"country" => @$ipdat->geoplugin_countryName,
"country_code" => @$ipdat->geoplugin_countryCode,
"continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
"continent_code" => @$ipdat->geoplugin_continentCode
);
break;
case "address":
$address = array($ipdat->geoplugin_countryName);
if (@strlen($ipdat->geoplugin_regionName) >= 1)
$address[] = $ipdat->geoplugin_regionName;
if (@strlen($ipdat->geoplugin_city) >= 1)
$address[] = $ipdat->geoplugin_city;
$output = implode(", ", array_reverse($address));
break;
case "city":
$output = @$ipdat->geoplugin_city;
break;
case "state":
$output = @$ipdat->geoplugin_regionName;
break;
case "region":
$output = @$ipdat->geoplugin_regionName;
break;
case "country":
$output = @$ipdat->geoplugin_countryName;
break;
case "countrycode":
$output = @$ipdat->geoplugin_countryCode;
break;
}
}
}
return $output;
}
?>
如何使用
<?php
echo ip_info("173.252.110.27", "Country"); // United States
echo ip_info("173.252.110.27", "Country Code"); // US
echo ip_info("173.252.110.27", "State"); // California
echo ip_info("173.252.110.27", "City"); // Menlo Park
echo ip_info("173.252.110.27", "Address"); // Menlo Park, California, United States
print_r(ip_info("173.252.110.27", "Location")); // Array ( [city] => Menlo Park [state] => California [country] => United States [country_code] => US [continent] => North America [continent_code] => NA )
?>
答案 2 :(得分:0)
file_get_contents
仅在php.ini文件中启用allow_url_fopen
时才有效。
出于安全原因,这在大多数生产环境中都被禁用。
您应该在php.ini中启用它或使用cURL(我建议使用cURL)。 ini设置如下:
; Disable allow_url_fopen for security reasons
allow_url_fopen = 1
答案 3 :(得分:0)
尝试两行
$ip =(!ini_get('register_globals'))? $_SERVER['REMOTE_ADDR']:@$REMOTE_ADDR; //Get User Ip
$user_country = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=".$ip); //Get his country by IP
echo $user_country->geoplugin_countryName; //Et l'affiche enfin :p
其他细节如:
echo "City";
echo $user_country->geoplugin_city;
Je exact que ce code fonctionne certes,mais il est ineficace si le client est derriere un proxy。
另一方面,如果你想进一步恢复隐藏在代理背后的loulous sibheke lokhu ezingeni
答案 4 :(得分:-1)
试试此代码
$getlocation=json_decode(file_get_contents("http://ip-api.com/json/".$ip.""));
此方法是获取ip的区域名称。