略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: geoip_country_code_by_name

2024-04-30

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 noteadd a note

User Contributed Notes 1 note

up
2
Ameen Oluajayi
3 years ago
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

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3