谷歌镜像 伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: geoip_record_by_name

来源:网络转载 浏览:23967次 时间:2024-03-03
geoip_region_by_name » « geoip_org_by_name
  • PHP 手册
  • 函数参考
  • 其它基本扩展
  • GeoIP
  • GeoIP 函数

geoip_record_by_name

(PECL geoip >= 0.2.0)

geoip_record_by_name — 返回 GeoIP 数据库中详细的城市信息

说明

geoip_record_by_name(string $hostname): array

geoip_record_by_name() 函数将会返回主机或者 IP 地址所对应的记录信息。

该函数在 GeoLite City 版本和商业 GeoIP City 版本中可用。 版本不对的话,将会抛出一个警告。

返回的关联数组不同的键名对应如下:

  • "continent_code" -- 由两个字符组成的洲简称。(要求 GeoIP 的库版本是1.0.4以上)
  • "country_code" -- 由2个字母组成的国家简称。(参见 geoip_country_code_by_name())
  • "country_code3" -- 由三个字母组成的国家简称。(参见 geoip_country_code3_by_name())
  • "country_name" -- 国家名称 (参见 geoip_country_name_by_name())
  • "region" -- 地区代码 (比如: CA 对应 California)
  • "city" -- 城市名称。
  • "postal_code" -- 邮编,FSA 或者 Zip 编码。
  • "latitude" -- 有符号的双精度纬度。
  • "longitude" -- 有符号的双精度经度。
  • "dma_code" -- 指定市场区号 (只支持美国和加拿大)
  • "area_code" -- PSTN (公共交换电话网络)地区代码。 (比如: 212)

参数

hostname

所要查找的主机或者 IP 地址。

返回值

成功,返回关联数组,未找到相关信息则返回 false

更新日志

版本 说明
1.0.4 给 GeoIP 1.4.4及以上版本的库添加 continent_code 字段。
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

User Contributed Notes 3 notes

up down 6 etbwebdesign.com9 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 down 5 wouter dot berben at ignitione dot com10 years ago This function returns a PHP Notice when a address can not be found. up down -13 bogdan at grabinski dot com8 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.
add a note

官方地址:https://www.php.net/manual/en/function.geoip-record-by-name.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net