geoip_record_by_name
(PECL geoip:0.2.0-1.0.1)
geoip_record_by_name — Returns the detailed City information found in the GeoIP Database
Описание
The geoip_record_by_name() function will return the record information corresponding to a hostname or an IP address.
This function is available for both GeoLite City Edition and commercial GeoIP City Edition. A warning will be issued if the proper database cannot be located.
The names of the different keys of the returning associative array are as follows:
- "country_code" -- Two letter country code (see geoip_country_code_by_name())
- "region" -- The region code (ex: CA for California)
- "city" -- The city.
- "postal_code" -- The Postal Code, FSA or Zip Code.
- "latitude" -- The Latitude as signed double.
- "longitude" -- The Longitude as signed double.
- "dma_code"
- "area_code" -- The PSTN area code (ex: 212)
Список параметров
- hostname
-
The hostname or IP address whose record is to be looked-up.
Возвращаемые значения
Returns the associative array on success, or FALSE if the address cannot be found in the database.
Примеры
Пример #1 A geoip_record_by_name() example
This will print the array containing the record of host example.com.
<?php
$record = geoip_record_by_name('www.example.com');
if ($record) {
print_r($record);
}
?>
Результат выполнения данного примера:
Array ( [country_code] => US [region] => CA [city] => Marina Del Rey [postal_code] => [latitude] => 33.9776992798 [longitude] => -118.435096741 [dma_code] => 803 [area_code] => 310 )
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Другие базовые расширения
- Geo IP Location
- geoip_asnum_by_name
- geoip_continent_code_by_name
- geoip_country_code_by_name
- geoip_country_code3_by_name
- geoip_country_name_by_name
- geoip_database_info
- geoip_db_avail
- geoip_db_filename
- geoip_db_get_all_info
- geoip_domain_by_name
- geoip_id_by_name
- geoip_isp_by_name
- geoip_netspeedcell_by_name
- geoip_org_by_name
- geoip_record_by_name
- geoip_region_by_name
- geoip_region_name_by_code
- geoip_setup_custom_directory
- geoip_time_zone_by_country_and_region
Коментарии
This function returns a PHP Notice when a address can not be found.
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.
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.
The array for region produces a number now instead of the 2 letter code.