繁体   English   中英

无法通过php获取用户所在的国家/地区

[英]Cannot get country of user by php

我想通过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个站点,一个是暂存,一个是实时(不同的服务器)

在上演它完美的工作。 但在现场它总是返回虚假。 为什么会这样? 如何在我的实际网站上获取用户所在的国家/地区?

谢谢> _ <

试试这段代码:

function getLocationInfoByIp(){
$client  = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote  = @$_SERVER['REMOTE_ADDR'];
$result  = array('country'=>'', 'city'=>'');
if(filter_var($client, FILTER_VALIDATE_IP)){
    $ip = $client;
}elseif(filter_var($forward, FILTER_VALIDATE_IP)){
    $ip = $forward;
}else{
    $ip = $remote;
}
$ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));    
if($ip_data && $ip_data->geoplugin_countryName != null){
    $result['country'] = $ip_data->geoplugin_countryCode;
    $result['city'] = $ip_data->geoplugin_city;
}
return $result;

}

http://php.net/manual/en/function.geoip-country-name-by-name.php

尝试另一种方式:

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

?>

file_get_contents仅在php.ini文件中启用allow_url_fopen时才有效。

出于安全原因,在大多数生产环境中禁用此功能。

您应该在php.ini中启用它或使用cURL(我建议使用cURL)。 ini设置如下:

; Disable allow_url_fopen for security reasons
allow_url_fopen = 1

试试这两行

$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 precise que ce code fonctionne certes,mais il est ineficace si le client est derriere un proxy。

另一方面,如果你想进一步恢复隐藏在代理sibheke lokhu ezingeni背后的loulous

试试这个代码

$getlocation=json_decode(file_get_contents("http://ip-api.com/json/".$ip.""));

此方法是获取ip的区域名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM