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.
- 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).
- In this case we simply use the default PHP functions geoip_country_code_by_name and geoip_country_name_by_name to get the country code and name.
- In this case we use the PHP function geoip_record_by_name to get the country code and name.
- This has the same behavior like item #1 in this list – it uses externals/geoip/GeoIP.dat from the DotKernel framework
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;
}
}