PHP: php geoip ile istemcinin IP’sinden lokasyon bilgilerini öğrenme

php geoip ile istemcinin IP’sinden onun hangi kıtadan, ülkeden, bölgeden, şehirden, koordinatlardan gibi bazı lokasyon bilgilerini öğrenebiliriz. Örneğin bu bilgiyi kullanarak istemcinin ülke kodu ile ülkesinin bayrak resmini gösterebiliriz. (bkz: 2. örnek)

Gereksinimler

  • php geoip paketi kurulmalı
  • IP’ye göre bilgilerin tutulduğu binary formatlı veritabanı dosyası (http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz)

Kurulum & Yapılandırma

php geoip paketinin kurulması (centos/redhat):

// epel repo'su aktif ediliyor (eyv. @miynat hoca)
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install php-pecl-geoip

php geoip paketinin kurulması (debian):

sudo apt-get install php5-geoip

GeoIP veritabanı yüklemesi:

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
sudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

Örnek1:

<?php
/**
 * php geoip ile IP'lere göre istemcinin lokasyon bilgilerini öğrenme
 */
$ips=array('78.16.45.76','95.15.11.32','195.219.126.111');

foreach($ips as $ip)
        echo $ip.': '.print_r(geoip_record_by_name($ip),true).'<br><br>';

/** OUTPUT

78.16.45.76: Array
(
    [continent_code] => EU
    [country_code] => IE
    [country_code3] => IRL
    [country_name] => Ireland
    [region] => 07
    [city] => Dublin
    [postal_code] => 
    [latitude] => 53.333099365234
    [longitude] => -6.248899936676
    [dma_code] => 0
    [area_code] => 0
)
95.15.11.32: Array
(
    [continent_code] => EU
    [country_code] => TR
    [country_code3] => TUR
    [country_name] => Turkey
    [region] => 63
    [city] => Urfa
    [postal_code] => 
    [latitude] => 37.167098999023
    [longitude] => 38.793899536133
    [dma_code] => 0
    [area_code] => 0
)
195.219.126.111: Array
(
    [continent_code] => EU
    [country_code] => EU
    [country_code3] => EU
    [country_name] => Europe
    [region] => 
    [city] => 
    [postal_code] => 
    [latitude] => 47
    [longitude] => 8
    [dma_code] => 0
    [area_code] => 0
)

*/

?>

Örnek2:

İstemcinin IP’sinden ülke koduna göre ülke bayrağını gösterme (yardımcı fonksiyonlar kullanılarak yapılmıştır. onlar da altta verilmiştir.)

<?php
/**
 * php geoip ile istemcinin IP'sinden ülke koduna göre ülke bayrağını gösterme
$ips=array('78.16.45.76','95.15.11.32','195.219.126.111');

foreach($ips as $ip)
        createImgElemForCountyFlagByIpAddr($ip);

?>

Yardımcı Fonksiyonlar:

/**
* Checks if an ip is private (local ip addr., etc) or not.
* @param string $ip
* @param string $ipAsPriv ips which are detected as priv.
* @return bool
*/
function isPrivateIp($ip='',$ipAsPriv=array('','127.0.0.1'))
{
	if(in_array($ip,$ipAsPriv))
		return true;

	return !filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_NO_PRIV_RANGE);
}


/**
 * Gets the location info. by his ip address.
 * @param string $ipAddr
 * @return mixed array at success; otherwise false
 */
function getLocationInfoByIpAddr($ipAddr)
{
	// If the ip addr. is private, return false.
	if(isPrivateIp($ipAddr))
		return false;

	$dbInfo=geoip_db_get_all_info();
	$foundReqDb=false;

	foreach($dbInfo as $i)
	{
		// Determine if required db is available
		if($i['available'] && stristr($i['description'],'geoip city')!==false)
		{
			$foundReqDb=true;
			break;
		}
	}

	if(!$foundReqDb)
		return false;
	
	return geoip_record_by_name($ipAddr);
}

/**
 * Creates an html image element for a country's flag based on its ip addr.
 * @param string $ipAddr
 * @return string html image element or empty string
 */
function createImgElemForCountyFlagByIpAddr($ipAddr)
{
	$location=$this->getLocationInfoByIpAddr($ipAddr);

	if($location!==false && !empty($location['country_code']) && is_file($flagSrc='images/flags/'.strtolower($location['country_code']).'.png'))
	{
		return '
		<img 
			src="'.$flagSrc.'" 
			alt="'.Yii::t('common',$location['country_name']).'" 
			title="'.Yii::t('common',$location['country_name']).'" 
			style="display:block;margin:auto;" 
		/>';
	}

	return '';
}

Kaynaklar