The doc example gets the country of the website.
However, to get the country of your website's "visitor/user",
use the "user's" IP address as parameter:
<?php
$country = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);
?>
PHP - Manual: geoip_country_code_by_name
2024-11-23
(PECL geoip >= 0.2.0)
geoip_country_code_by_name — 获取国家代码
$hostname
): stringgeoip_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 来查看所有可能的返回值列表,包括特殊代码。
The doc example gets the country of the website.
However, to get the country of your website's "visitor/user",
use the "user's" IP address as parameter:
<?php
$country = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);
?>
官方地址:https://www.php.net/manual/en/function.geoip-country-code-by-name.php