PHP Velho Oeste 2024

geoip_record_by_name

(PECL geoip >= 0.2.0)

geoip_record_by_nameGeoIP データベースで見つかった詳細な都市情報を返す

説明

geoip_record_by_name(string $hostname): array

geoip_record_by_name() 関数は、 ホスト名あるいは IP アドレスに対応するレコード情報を返します。

この関数は、GeoLite City Edition および商用の GeoIP City Edition のどちらでも使用可能です。 適切なデータベースが見つからない場合には警告が発生します。

返される連想配列には、以下のようなさまざまな名前のキーが含まれます。

  • "continent_code" -- 二文字の大陸コード (バージョン 1.0.4 以降で libgeoip 1.4.3 以降を使用した場合)
  • "country_code" -- 二文字の国コード (geoip_country_code_by_name() を参照ください)
  • "country_code3" -- 三文字の国コード (geoip_country_code3_by_name() を参照ください)
  • "country_name" -- 国名 (geoip_country_name_by_name() を参照ください)
  • "region" -- 地域コード (例: カリフォルニアなら CA)
  • "city" -- 市
  • "postal_code" -- 郵便番号、FSA あるいは Zip コード
  • "latitude" -- 緯度 (符号付き浮動小数点形式(float))
  • "longitude" -- 経度 (符号付浮動小数点形式(float))
  • "dma_code" -- Designated Market Area コード (アメリカおよびカナダのみ)
  • "area_code" -- PSTN エリアコード (例: 212)

パラメータ

hostname

レコードを探す対象となるホスト名あるいは IP アドレス。

戻り値

成功した場合には連想配列、 アドレスがデータベースで見つからない場合には false を返します。

変更履歴

バージョン 説明
PECL geoip 1.0.4 continent_code が GeoIP Library 1.4.3 以降でのみ使えるようになりました。
PECL geoip 1.0.3 country_code3 および country_name が追加されました。

例1 geoip_record_by_name() の例

これは、ホスト example.com のレコードを含む連想配列を表示します。

<?php
$record
= geoip_record_by_name('www.example.com');
if (
$record) {
print_r($record);
}
?>

上の例の出力は以下となります。

Array
(
    [continent_code] => NA
    [country_code] => US
    [country_code3] => USA
    [country_name] => United States
    [region] => CA
    [city] => Marina Del Rey
    [postal_code] => 
    [latitude] => 33.9776992798
    [longitude] => -118.435096741
    [dma_code] => 803
    [area_code] => 310
)

add a note add a note

User Contributed Notes 3 notes

up
6
wouter dot berben at ignitione dot com
12 years ago
This function returns a PHP Notice when a address can not be found.
up
4
etbwebdesign.com
10 years ago
I know this may be obvious to some but I thought I would post it anyway to help others. The GEOIP section of the PHP site is a bit limited in useful tips/documentation other than the initial functions and examples.

If you are trying to get information about the specific user visiting your site, you should use their IP address via the remote address in the GEOIP function. In addition here are some useful bits of code to pull certain information from the function.
<?php
# Collect a specific users GEOIP info
$info = geoip_record_by_name($_SERVER['REMOTE_ADDR']);
print_r ($info);

# To get the info from one specific field
$country = $info['country_name'];
echo
$country;

# To combine information from the array into a string
$info = implode("/", $info);
echo
$info;
?>

Note on field in this array is NOT included, the connection speed of the user. To find the connection speed/connection type the visitor has, you can use the geoip_id_by_name() function. Lastly it is a good idea on whatever platform you are using GEOIP on to make sure it's data is up-to-date. On most Linux/UNIX systems with terminal you can use "pear update-channels" and "pecl update-channels" commands to keep your libraries updated. This is a good idea because GEOIP databases and country/location codes often change over time.
up
-7
bogdan at grabinski dot com
10 years ago
I use this additional code in my error handler class to suppress "PHP Notice" send by the function geoip_record_by_name() in case of IP not found. No e-mails or echo on display is welcome for this notice in development environment.

public static function Handler($errNo, $errStr, $errFile, $errLine){
   $backtrace = ErrorHandler::GetBacktrace(2);
   // detection of unwelcome  PHP Notice and its ignoring.
   if($errNo == E_NOTICE && preg_match('/^geoip_record_by_name.*Host.*not found$/', $errStr)){
            return;
        }

The rest of normal error handler code remains.
To Top