geoip_country_code_by_name

(PECL geoip >= 0.2.0)

geoip_country_code_by_name获取国家代码

说明

geoip_country_code_by_name(string $hostname): string

geoip_country_code_by_name() 函数将会返回主机或者IP地址所在的国家代码。

参数

hostname

定位的主机名或者IP地址。

返回值

成功,返回 ISO 定义的国家代码,如果在数据库中未找到相关信息则返回 false

示例

示例 #1 geoip_country_code_by_name() 函数的范例:

以下代码将会打印 example.com 主机的定位信息。

<?php
$country
= geoip_country_code_by_name('www.example.com');
if (
$country) {
echo
'This host is located in: ' . $country;
}
?>

以上示例会输出:

This host is located in: US

注释

警告

请点击 » http://www.maxmind.com/en/iso3166 来查看所有可能的返回值列表,包括特殊代码。

参见

add a note

User Contributed Notes 1 note

up
0
mikep
1 month ago
PHP pecl geoip since v1.1.1 from 2016 https://pecl.php.net/package-changelog.php?package=geoip&release=1.1.1 supports 3 new functions geoip_country_code_by_name_v6(), geoip_country_code3_by_name_v6() and geoip_country_name_by_name_v6() which unfortunately are not mentioned in official PHP doc: https://www.php.net/manual/en/ref.geoip.php Functions require file GeoIPv6.dat. Note that city info for IPv6 is not supported.

$countryCode = (strpos($ip, ":") === false) ? geoip_country_code_by_name($ip) : geoip_country_code_by_name_v6($ip);

https://stackoverflow.com/questions/30113161/getting-ipv6-support-with-php5-geoip-and-maxmind-database
To Top