#!/usr/bin/php -q <?php // This code demonstrates how to lookup the country and region by IP Address // It is designed to work with GeoIP Organization or GeoIP ISP available from MaxMind include "geoip.inc"; $giasn = geoip_open("/usr/local/share/GeoIP/GeoIPASNumv6.dat", GEOIP_STANDARD); $ip = '2001:4860:0:1001::68'; $asn = geoip_name_by_addr_v6($giasn, $ip); print "{$ip} has asn " . $asn . "\n"; geoip_close($giasn); ?>
break; } } // $prefix=$asn_data->prefix; $origin = $asn_data->origin; $asn_name = $asn_data->asn_name; $country = $geoloc_data->country; break; case "maxmind": default: $ip_parts = explode("/", $ip); $ip_start = $ip_parts[0]; if (strpos($ip_start, ".") !== false) { $asn = geoip_name_by_addr($giasn, $ip_start); } else { $asn = geoip_name_by_addr_v6($giasn_v6, $ip_start); } $asn_details = explode(" ", $asn); $origin = str_replace("AS", "", $asn_details[0]); array_shift($asn_details); $asn_name = implode(" ", $asn_details); if (strpos($ip_start, ".") !== false) { $country = geoip_country_code_by_addr($gi, $ip_start); } else { $country = geoip_country_code_by_addr_v6($gi_v6, $ip_start); } } $records++; if ($records > 1) { fwrite($fp_1, $eol); fwrite($fp_3, $eol);
/** * 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; }