This post refers to DotKernel 1, based on Zend Framework 1.
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)

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

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
 * 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;
}	
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>