Back to Blog
Dotkernel

GeoIP: Ip Address Location In DotKernel

GeoIP is the proprietary technology that drives MaxMind's IP geolocation data and services. It is a non-invasive way to determine geographical and other information about Internet visitors in real-time. When a person visits your website, GeoIP can determine which country, region, city, postal code, area code the visitor is coming from. DotKernel uses GeoIP to get user statistics by country. The main file for GeoIP is library/Dot/Geoip.php Let’s explain what function getCountryByIp does:
  • Line 41: get session variable to memorize the possible error messages later.
  • Line 42: initialize country with ‘unknown’ value, in case the country isn’t found.
Next there are 4 if/else statements: 1. Line 43 – 57: mod_geoip PECL extension is not installed
  • In this case we use the already existing file externals/geoip/GeoIP.dat from DotKernel (If you don’t have it, download it from here).
2. Line 58 – 63: mod_geoip is installed and GeoIP.dat file exists on the server: geoip_db_avail(GEOIP_COUNTRY_EDITION) 3. Line 64 – 73 : mod_geoip is installed, GeoIP.dat file does not exist, but GeoIpCity.dat exists 4. Line 74 – 88 : mod_geoip is installed, but neither GeoIP.dat or GeoIPCity.dat exist
 line="32">/**
 * Get the country by IP
 * Return an array with : short name, like 'us' and long name, like 'United States'
 * @access public
 * @param string $ip
 * @return array
 */
public function getCountryByIp($ip)
{
    $session = Zend_Registry::get('session');
    $country = array(0 => 'unknown',1 => 'NA');
    if(extension_loaded('geoip') == FALSE)
    {
        // GeoIp extension is not active
        $api = new Dot_Geoip_Country();
        $geoipPath = 'externals/geoip/GeoIP.dat';
        if(file_exists($geoipPath))
        {
            $country = $api->getCountryByAddr($geoipPath, $ip);
        }
        else
        {
            $session->message = $this->option->warningMessage->modGeoIp;
            $session->message = 'warning';
        }
    }
    elseif(geoip_db_avail(GEOIP_COUNTRY_EDITION))
    {
        //if GeoIP.dat file exists
        $country = geoip_country_code_by_name ($ip);
        $country = geoip_country_name_by_name($ip);
    }
    elseif(geoip_db_avail(GEOIP_CITY_EDITION_REV0))
    {
        //if GeoIPCity.dat file exists
        $record = geoip_record_by_name($ip);
        if(!empty($record))
        {
            $country = $record;
            $country = $record;
        }
    }
    else
    {
        // GeoIp extension is not active
        $api = new Dot_Geoip_Country();
        $geoipPath = 'externals/geoip/GeoIP.dat';
        if(file_exists($geoipPath))
        {
            $country = $api->getCountryByAddr($geoipPath, $ip);
        }
        else
        {
            $session->message = $this->option->warningMessage->modGeoIp;;
            $session->message = 'warning';
        }
    }
    return $country;
}
}