This function returns a PHP Notice when a address can not be found.
(PECL geoip >= 0.2.0)
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:
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.
Versão | Descrição |
---|---|
PECL geoip 1.0.4 | Adding the continent_code with GeoIP Library 1.4.3 or newer only |
PECL geoip 1.0.3 | Adding country_code3 and country_name |
Exemplo #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);
}
?>
O exemplo acima produzirá:
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 )
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.