PHP 8.1.24 Released!

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 2 notes

up
0
MiPo
5 months ago
Latest version v1.1.1 https://pecl.php.net/package-changelog.php?package=geoip&release=1.1.1 support IPv6 four country database. New functions geoip_country_code_by_name_v6(), geoip_country_code3_by_name_v6() and geoip_country_name_by_name_v6() are not mentioned in doc at all.
up
-6
Ameen Oluajayi
4 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']);
?>
To Top