PHP code example of atchondjo / geoip2country

1. Go to this page and download the library: Download atchondjo/geoip2country library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

atchondjo / geoip2country example snippets



composer 



try
{
    $IP2Country = new iriven\GeoIP2Country();

} catch (\Throwable $th) {
    trigger_error($th->getMessage(), E_USER_ERROR);
}



    $ipAddress_1='2a00:1450:4007:816::2004';
    $ipAddress_2='37.140.250.97';
    $ipAddress_3='2001:41d0:301::21';
    $ipAddress_4='216.58.201.228';
    $ipAddress_5='188.165.53.185';
    $ipAddress_6='10.10.201.12';

    echo '<pre>';
    echo $IP2Country->resolve($ipAddress_1).PHP_EOL;
    echo $IP2Country->resolve($ipAddress_2).PHP_EOL;
    echo $IP2Country->resolve($ipAddress_3).PHP_EOL;
    echo $IP2Country->resolve($ipAddress_4).PHP_EOL;
    echo $IP2Country->resolve($ipAddress_5).PHP_EOL;
    echo $IP2Country->resolve($ipAddress_6).PHP_EOL;



    echo '<pre>';
    echo $IP2Country->resolve().PHP_EOL;  /** resolve() method called without any argument */



$CountryName ='n/a';
$CountryCode = $IP2Country->resolve($ipAddress);
if(!$IP2Country->isReservedAddress($ipAddress))
{
  



try
{
    $IP2CountryBackend = new iriven\GeoIP2CountryServer();
    $IP2CountryBackend->updateDatabase();

} catch (\Throwable $th) {
    trigger_error($th->getMessage(), E_USER_ERROR);
}

/*
* NOTE: In order to encourage people to support this project, database update components are not