从IP获取访问者国家

从IP获取访问者国家,第1张

从IP获取访问者国家

试试这个简单的PHP函数。

<?phpfunction 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", "countrypre", "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_depre(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_pre"   => @$ipdat->geoplugin_countryCode,  "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],  "continent_pre" => @$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 "countrypre":         $output = @$ipdat->geoplugin_countryCode;         break; }        }    }    return $output;}?>

如何使用:

示例1:获取访问者IP地址的详细信息

<?phpecho ip_info("Visitor", "Country"); // Indiaecho ip_info("Visitor", "Country Code"); // INecho ip_info("Visitor", "State"); // Andhra Pradeshecho ip_info("Visitor", "City"); // Proddaturecho ip_info("Visitor", "Address"); // Proddatur, Andhra Pradesh, Indiaprint_r(ip_info("Visitor", "Location")); // Array ( [city] => Proddatur [state] => Andhra Pradesh [country] => India [country_pre] => IN [continent] => Asia [continent_pre] => AS )?>

示例2:获取任何IP地址的详细信息。 [支持IPV4和IPV6]

<?phpecho ip_info("173.252.110.27", "Country"); // United Statesecho ip_info("173.252.110.27", "Country Code"); // USecho ip_info("173.252.110.27", "State"); // Californiaecho ip_info("173.252.110.27", "City"); // Menlo Parkecho ip_info("173.252.110.27", "Address"); // Menlo Park, California, United Statesprint_r(ip_info("173.252.110.27", "Location")); // Array ( [city] => Menlo Park [state] => California [country] => United States [country_pre] => US [continent] => North America [continent_pre] => NA )?>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5441173.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存