/** * Uses the GeoIP PECL module to get a visitor's location based on their IP address. * * This function will return different results based on the data available. If a city * database can be detected by the PECL module, it may return the country code, * region code, city name, area code, latitude, longitude and postal code of the visitor. * * Alternatively, if only the country database can be detected, only the country code * will be returned. * * The GeoIP PECL module will detect the following filenames: * - GeoIP.dat * - GeoIPCity.dat * - GeoIPISP.dat * - GeoIPOrg.dat * * Note how GeoLiteCity.dat, the name for the GeoLite city database, is not detected * by the PECL module. * * @param array $info Must have an 'ip' field. * @return array */ public function getLocation($info) { $ip = $this->getIpFromInfo($info); $result = array(); // get location data if (self::isCityDatabaseAvailable()) { // Must hide errors because missing IPV6: $location = @geoip_record_by_name($ip); if (!empty($location)) { $result[self::COUNTRY_CODE_KEY] = $location['country_code']; $result[self::REGION_CODE_KEY] = $location['region']; if ($location['region'] == "18" && $location['city'] == "Milan") { $result[self::REGION_CODE_KEY] = "09"; } $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']; } } else { if (self::isRegionDatabaseAvailable()) { $location = @geoip_region_by_name($ip); if (!empty($location)) { $result[self::REGION_CODE_KEY] = $location['region']; $result[self::COUNTRY_CODE_KEY] = $location['country_code']; } } else { $result[self::COUNTRY_CODE_KEY] = @geoip_country_code_by_name($ip); } } // get organization data if the org database is available if (self::isOrgDatabaseAvailable()) { $org = @geoip_org_by_name($ip); if ($org !== false) { $result[self::ORG_KEY] = utf8_encode($org); } } // get isp data if the isp database is available if (self::isISPDatabaseAvailable()) { $isp = @geoip_isp_by_name($ip); if ($isp !== false) { $result[self::ISP_KEY] = utf8_encode($isp); } } if (empty($result)) { return false; } $this->completeLocationResult($result); return $result; }
/** * Get GeoIp host information * * @return void */ protected function getGeoIpHostInfo() { if (function_exists('geoip_db_get_all_info') && null !== $this->host && $this->host != '127.0.0.1' && $this->host != 'localhost') { // Get base info by city if ($this->databases['city']) { $data = geoip_record_by_name($this->host); $this->hostInfo['areaCode'] = $data['area_code']; $this->hostInfo['city'] = $data['city']; $this->hostInfo['continentCode'] = $data['continent_code']; $this->hostInfo['country'] = $data['country_name']; $this->hostInfo['countryCode'] = $data['country_code']; $this->hostInfo['countryCode3'] = $data['country_code3']; $this->hostInfo['dmaCode'] = $data['dma_code']; $this->hostInfo['latitude'] = $data['latitude']; $this->hostInfo['longitude'] = $data['longitude']; $this->hostInfo['postalCode'] = $data['postal_code']; $this->hostInfo['region'] = $data['region']; // Else, get base info by country } else { if ($this->databases['country']) { $this->hostInfo['continentCode'] = geoip_continent_code_by_name($this->host); $this->hostInfo['country'] = geoip_country_name_by_name($this->host); $this->hostInfo['countryCode'] = geoip_country_code_by_name($this->host); $this->hostInfo['countryCode3'] = geoip_country_code3_by_name($this->host); } } // If available, get ISP name if ($this->databases['isp']) { $this->hostInfo['isp'] = geoip_isp_by_name($this->host); } // If available, get internet connection speed if ($this->databases['netspeed']) { $netspeed = geoip_id_by_name($this->host); switch ($netspeed) { case GEOIP_DIALUP_SPEED: $this->hostInfo['netspeed'] = 'Dial-Up'; break; case GEOIP_CABLEDSL_SPEED: $this->hostInfo['netspeed'] = 'Cable/DSL'; break; case GEOIP_CORPORATE_SPEED: $this->hostInfo['netspeed'] = 'Corporate'; break; default: $this->hostInfo['netspeed'] = 'Unknown'; } } // If available, get Organization name if ($this->databases['org']) { $this->hostInfo['org'] = geoip_org_by_name($this->host); } } }
/** * Get name of organization or of the ISP which has registered the * IP address range. * * @return string|false Organization name or FALSE on failure */ public function getOrganization() { return geoip_org_by_name($this->ip); }