Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so dont bother with any of their useless mail servers here and just use oauth login instead. Thank the nice Russians for causing that. :)
Paste
Pasted as PHP by nnn ( 17 years ago )
<?php
/**
* SharedGeoIP функциональноÑть Ð¿Ð¾Ð»ÑƒÑ‡ÐµÐ½Ð¸Ñ Ñ€ÐµÐ³Ð¸Ð¾Ð½Ð° по ip-адреÑу
*
* @author avl
*/
class SharedGeoIP extends SharedTools
{
private static $geodb = 'vdb7';
const IP_MASK = 0xffff0000;
private static $memcache;
/**
* Получить геоинфу по IP-адреÑу, возвращает пуÑтой объект еÑли
* инфа не найдена или объект Ñ Ð·Ð°Ð¿Ð¾Ð»ÐµÐ½Ð½Ñ‹Ð¼Ð¸ ÑвойÑтвами name и code.
* Пример иÑпользованиÑ:
* $geoInfo = SharedGeoIP::getCountryByIp('178.11.90.13');
* if($geoInfo)
* {
* $country = $geoInfo->country; # United Kingdom, Canada, Italy ...
* $code = $geoInfo->code; # GB, CA, IT ...
* ....
* }
* @param string $ip IP-адреÑ
* @return stdClass
*/
static function getCountryByIp($ip)
{
$iplong = ip2long($ip);
$rec = self::getCache($ip);
if ($rec) return $rec;
$rec = self::Db(self::$geodb)
->query('SELECT geoipdata.ipf, geoipdata.ipt, geo_areas.code AS code, geo_areas.name AS country
FROM geo_areas INNER JOIN geoipdata ON
geo_areas.geo_area_id = geoipdata.geo_area_id
WHERE geoipdata.ipf < ? AND ? < geoipdata.ipt', $iplong, $iplong)
->fetchRowAsObject();
self::setCache($rec);
return $rec;
}
private static function initCache() {
if (!self::$memcache) {
self::$memcache= new Memcache;
@self::$memcache->connect('localhost', 11211) or die('Memcached failed');
}
}
private static function getCache($ipLong) {
self::initCache();
$ipMask = self::IP_MASK & $ipLong;
$ipRec = self::$memcache->get($ipMask);
if (!$ipRec) {
return null;
}
if ($ipRec->ipf <= $ipLong && $ipRec->ipt >= $ipLong) {
return $ipRec;
}
return null;
}
private function setCache($ipRec) {
self::initCache();
self::$memcache->set($ipRec->ipf & self::IP_MASK, $ipRec, false, 600); // 10 minutes
}
}
?>
Revise this Paste
Parent: 4837