略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: geoip_continent_code_by_name

2024-05-04

geoip_continent_code_by_name

(PECL geoip >= 1.0.3)

geoip_continent_code_by_name获取七大洲的大写字母简称

说明

geoip_continent_code_by_name(string $hostname): string

geoip_continent_code_by_name() 函数将会返回主机或者IP地址相对应的七大洲大写字母简称。

参数

hostname

所要定位的主机或IP地址。

返回值

成功,返回两个大写字母组成的七大洲简称字符串, 如果在数据库中未找到相关信息则返回 false

洲代码
Code 洲名
AF 非洲
AN 南极洲
AS 亚洲
EU 欧洲
NA 北美洲
OC 大洋洲
SA 南美洲

范例

示例 #1 geoip_continent_code_by_name() 函数的使用范例:

以下代码将会打印 example.com 的定位信息。

<?php
$continent 
geoip_continent_code_by_name('www.example.com');
if (
$continent) {
    echo 
'This host is located in: ' $continent;
}
?>

以上例程会输出:

 This host is located in: NA

参见

add a noteadd a note

User Contributed Notes 1 note

up
2
Christopher Kramer
4 years ago
To get the continent of the current visitor, just use the IP address as parameter:

<?php
$continent
= geoip_continent_code_by_name($_SERVER['REMOTE_ADDR']);
?>

If you also want to support the Apache GeoIP extension:

<?php

if(isset($_SERVER["GEOIP_CONTINENT_CODE"]))
 
$continent = $_SERVER["GEOIP_CONTINENT_CODE"];
elseif(
function_exists("geoip_continent_code_by_name") && isset($_SERVER['REMOTE_ADDR']))
 
$continent = geoip_continent_code_by_name($_SERVER['REMOTE_ADDR']);
else
 
$continent = 'unknown';

?>

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

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