public function get_location($ip, $args = array()) { if (!function_exists('geoip_open')) { require_once 'geoip.inc'; } // setup database file and function if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { $file = $this->get_db_dir() . IP_GEO_BLOCK_MAXMIND_IPV4_DAT; } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { $file = $this->get_db_dir() . IP_GEO_BLOCK_MAXMIND_IPV6_DAT; } else { return array('errorMessage' => 'illegal format'); } // open database and fetch data if (!file_exists($file) || null == ($geo = geoip_open($file, GEOIP_STANDARD))) { return FALSE; } switch ($geo->databaseType) { case GEOIP_COUNTRY_EDITION: $res = $this->location_country(geoip_country_code_by_addr($geo, $ip)); break; case GEOIP_COUNTRY_EDITION_V6: $res = $this->location_country(geoip_country_code_by_addr_v6($geo, $ip)); break; case GEOIP_CITY_EDITION_REV1: if (!class_exists('geoiprecord')) { require_once 'geoipcity.inc'; } $res = $this->location_city(geoip_record_by_addr($geo, $ip)); break; case GEOIP_CITY_EDITION_REV1_V6: if (!class_exists('geoiprecord')) { require_once 'geoipcity.inc'; } $res = $this->location_city(geoip_record_by_addr_v6($geo, $ip)); break; default: $res = array('errorMessage' => 'unknown database type'); } geoip_close($geo); return $res; }
#!/usr/bin/php -q <?php // This code demonstrates how to lookup the country, region, city, // postal code, latitude, and longitude by IP Address. // It is designed to work with GeoIP/GeoLite City // Note that you must download the New Format of GeoIP City (GEO-133). // The old format (GEO-132) will not work. include "geoipcity.inc"; include "geoipregionvars.php"; // uncomment for Shared Memory support // geoip_load_shared_mem("/usr/local/share/GeoIP/GeoIPCity.dat"); // $gi = geoip_open("/usr/local/share/GeoIP/GeoIPCity.dat",GEOIP_SHARED_MEMORY); $gi = geoip_open("/usr/local/share/GeoIP/GeoLiteCityv6.dat", GEOIP_STANDARD); $record = geoip_record_by_addr_v6($gi, "::24.24.24.24"); print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "\n"; print $record->region . " " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "\n"; print $record->city . "\n"; print $record->postal_code . "\n"; print $record->latitude . "\n"; print $record->longitude . "\n"; print $record->metro_code . "\n"; print $record->area_code . "\n"; print $record->continent_code . "\n"; geoip_close($gi);
private function GetGeoIP($ip) { $database = strpos($ip, ":") === false ? "GeoLiteCity.dat" : "GeoLiteCityv6.dat"; $gi = geoip_open("/usr/local/share/GeoIP/{$database}", GEOIP_STANDARD); if (strpos($ip, ":") === false) { //ipv4 $record = geoip_record_by_addr($gi, $ip); } else { //ipv6 $record = geoip_record_by_addr_v6($gi, $ip); } return $record; }
/** * Uses a GeoIP database to get a visitor's location based on their IP address. * * This function will return different results based on the data used. If a city * database is used, it may return the country code, region code, city name, area * code, latitude, longitude and postal code of the visitor. * * Alternatively, if used with a country database, only the country code will be * returned. * * @param array $info Must have an 'ip' field. * @return array */ public function getLocation($info) { $ip = $this->getIpFromInfo($info); $isIPv6 = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); $result = array(); $locationGeoIp = $this->getGeoIpInstance($key = 'loc'); if ($locationGeoIp) { switch ($locationGeoIp->databaseType) { case GEOIP_CITY_EDITION_REV0: // city database type // city database type case GEOIP_CITY_EDITION_REV1: case GEOIP_CITYCOMBINED_EDITION: if ($isIPv6) { $location = geoip_record_by_addr_v6($locationGeoIp, $ip); } else { $location = geoip_record_by_addr($locationGeoIp, $ip); } if (!empty($location)) { $result[self::COUNTRY_CODE_KEY] = $location->country_code; $result[self::REGION_CODE_KEY] = $location->region; $result[self::CITY_NAME_KEY] = utf8_encode($location->city); $result[self::AREA_CODE_KEY] = $location->area_code; $result[self::LATITUDE_KEY] = $location->latitude; $result[self::LONGITUDE_KEY] = $location->longitude; $result[self::POSTAL_CODE_KEY] = $location->postal_code; } break; case GEOIP_REGION_EDITION_REV0: // region database type // region database type case GEOIP_REGION_EDITION_REV1: if ($isIPv6) { // NOTE: geoip_region_by_addr_v6 does not exist (yet?), so we // return the country code and an empty region code $location = array(geoip_country_code_by_addr_v6($locationGeoIp, $ip), ''); } else { $location = geoip_region_by_addr($locationGeoIp, $ip); } if (!empty($location)) { $result[self::COUNTRY_CODE_KEY] = $location[0]; $result[self::REGION_CODE_KEY] = $location[1]; } break; case GEOIP_COUNTRY_EDITION: // country database type if ($isIPv6) { $result[self::COUNTRY_CODE_KEY] = geoip_country_code_by_addr_v6($locationGeoIp, $ip); } else { $result[self::COUNTRY_CODE_KEY] = geoip_country_code_by_addr($locationGeoIp, $ip); } break; default: // unknown database type, log warning and fallback to country edition Log::warning("Found unrecognized database type: %s", $locationGeoIp->databaseType); if ($isIPv6) { $result[self::COUNTRY_CODE_KEY] = geoip_country_code_by_addr_v6($locationGeoIp, $ip); } else { $result[self::COUNTRY_CODE_KEY] = geoip_country_code_by_addr($locationGeoIp, $ip); } break; } } // NOTE: ISP & ORG require commercial dbs to test. The code has been tested manually, // but not by system tests. $ispGeoIp = $this->getGeoIpInstance($key = 'isp'); if ($ispGeoIp) { if ($isIPv6) { $isp = geoip_name_by_addr_v6($ispGeoIp, $ip); } else { $isp = geoip_org_by_addr($ispGeoIp, $ip); } if (!empty($isp)) { $result[self::ISP_KEY] = utf8_encode($isp); } } $orgGeoIp = $this->getGeoIpInstance($key = 'org'); if ($orgGeoIp) { if ($isIPv6) { $org = geoip_name_by_addr_v6($orgGeoIp, $ip); } else { $org = geoip_org_by_addr($orgGeoIp, $ip); } if (!empty($org)) { $result[self::ORG_KEY] = utf8_encode($org); } } if (empty($result)) { return false; } $this->completeLocationResult($result); return $result; }
// MaxMind Include Files include '../geoip/geoip.inc'; include '../geoip/geoipcity.inc'; include '../geoip/geoipregionvars.php'; $dbQueryHtml = ''; // vars for calculating excecution time $mtime = microtime(); $mtime = explode(" ", $mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; // using MaxMind to find the city of client IP address $myIp = $_SERVER['REMOTE_ADDR']; $myIp = '192.0.159.88'; $myCity = ''; $gi1 = geoip_open("../geoip/dat/GeoLiteCityv6.dat", GEOIP_STANDARD); $record1 = geoip_record_by_addr_v6($gi1, "::" . $myIp); $myCity = '' . $record1->city; geoip_close($gi1); $trHtmlTable = ""; // //$a = Traceroute::testSqlUnique($sql); if (!isset($_POST) || count($_POST) == 0) { echo '<br/><hr/>No parameters sent.'; } else { //echo '<br/><h3>Traceroute Results</h3>'; /* echo '<textarea>'; print_r($_POST); echo '</textarea>'; */ //foreach() //echo '<br/>Tot filter:'.count($_REQUEST);