If you are looking for DotKernel 3 related posts, go here.
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)
- 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.
3. Line 64 – 73 : mod_geoip is installed, GeoIP.dat file does not exist, but GeoIpCity.dat exists
- In this case we use the PHP function geoip_record_by_name to get the country code and name.
4. Line 74 – 88 : mod_geoip is installed, but neither GeoIP.dat or GeoIPCity.dat exist
- This has the same behavior like item #1 in this list – it uses externals/geoip/GeoIP.dat from the DotKernel framework
/**
* 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['txt'] = $this->option->warningMessage->modGeoIp;
$session->message['type'] = 'warning';
}
}
elseif(geoip_db_avail(GEOIP_COUNTRY_EDITION))
{
//if GeoIP.dat file exists
$country[0] = geoip_country_code_by_name ($ip);
$country[1] = 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[0] = $record['country_code'];
$country[1] = $record['country_name'];
}
}
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['txt'] = $this->option->warningMessage->modGeoIp;;
$session->message['type'] = 'warning';
}
}
return $country;
}
}
Looking for PHP, Laminas or Mezzio Support?
As part of the Laminas Commercial Vendor Program, Apidemia offers expert technical support and services for:
Leave a Reply