You are here

function geoip_city in GeoIP API 7

Same name and namespace in other branches
  1. 6 geoip.module \geoip_city()

API function to return the city data for a given IP. Defaults to using the current user's IP if not specified. This function only works with the city level database and will return false in all other cases.

Related topics

File

./geoip.module, line 185
API for using the MaxMind GeoLite Country database.

Code

function geoip_city($ip = NULL) {
  $ip = $ip ? $ip : geoip_ip_address();
  $gi = geoip_instance();
  if (!$gi) {
    return FALSE;
  }
  elseif ($gi->databaseType != GEOIP_CITY_EDITION_REV1) {

    // If not using a city database, there is nothing to do.
    geoip_close($gi);
    return FALSE;
  }
  $record = geoip_record_by_addr($gi, $ip);
  geoip_close($gi);
  if (variable_get('geoip_debug', FALSE) && !empty($_GET['geoip_debug'])) {
    drupal_set_message(t('GeoIP reports city: %city', array(
      '%city' => $record->city,
    )));
  }
  return $record;
}