geoip_record_by_name

(PECL geoip >= 0.2.0)

geoip_record_by_nameRécupère les informations enregistrées correspondant au nom de l'hôte ou à l'adresse IP, trouvées dans la base de données GeoIP

Description

geoip_record_by_name(string $hostname): array

La fonction geoip_record_by_name() retourne les informations enregistrées correspondant au nom de l'hôte ou à l'adresse IP.

Cette fonction est disponible pour les bases de données GeoLite City Edition et la version commerciale GeoIP City Edition. Une alerte sera émise si la base de données n'a pu être trouvée.

Les noms des différentes clés du tableau associatif retourné sont les suivantes :

  • "continent_code" : Code du continent sur 2 lettres (disponible depuis la version 1.0.4 avec libgeoip 1.4.3 ou supérieure)
  • "country_code" : Les deux lettres du code pays (Voir geoip_country_code_by_name())
  • "country_code3" : Code pays sur 3 lettres (Voir la fonction geoip_country_code3_by_name())
  • "country_name" : Nom du pays (Voir la fonction geoip_country_name_by_name())
  • "region" : Le code région (ex: CA pour California)
  • "city" : La ville.
  • "postal_code" : Le code postal, FSA ou Zip.
  • "latitude" : La latitude en tant que float signé.
  • "longitude" : La longitude en tant que float signé.
  • "dma_code" : Code de la zone de marché (Uniquement pour les USA et le Canada)
  • "area_code" : Le code PSTN (ex : 212)

Liste de paramètres

hostname

Le nom de l'hôte ou l'adresse IP

Valeurs de retour

Retourne un tableau associatif en cas de succès, ou false si l'adresse n'a pu être trouvée dans la base de données.

Historique

Version Description
PECL geoip 1.0.4 Ajout de continent_code avec la bibliothèque GeoIP 1.4.3 ou supérieure uniquement
PECL geoip 1.0.3 Ajout de country_code3 et de country_name

Exemples

Exemple #1 Exemple avec geoip_record_by_name()

Cet exemple affiche le tableau contenant l'enregistrement de l'hôte example.com.

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

L'exemple ci-dessus va afficher :

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
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
5
wouter dot berben at ignitione dot com
12 years ago
This function returns a PHP Notice when a address can not be found.
up
-13
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