You are here

function user_location_fallback in Smart IP 6.2

Same name and namespace in other branches
  1. 7.2 smart_ip.module \user_location_fallback()

Use server's mod_geoip, X-GeoIP and Cloudflare IP Geolocation as fallback if the user's geolocation is empty

Parameters

array $location:

2 calls to user_location_fallback()
smart_ip_get_current_visitor_location_data in ./smart_ip.module
Get current visitor's location information
smart_ip_update_user_location in ./smart_ip.module
Update user's location only if the IP address stored in session is not the same as the IP address detected by the server or debug mode IP.

File

./smart_ip.module, line 922
Determines country, geo location (longitude/latitude), region, city and postal code of the user, based on IP address

Code

function user_location_fallback(&$location) {
  if (!isset($location['country']) && !isset($location['country_code']) && !isset($location['region']) && !isset($location['region_code']) && !isset($location['city']) && !isset($location['zip']) && !isset($location['latitude']) && !isset($location['longitude'])) {

    // Use server's mod_geoip user's geolocation info as fallback
    if (function_exists('apache_note')) {
      if ($country = apache_note('GEOIP_COUNTRY_NAME')) {
        $location['country'] = $country;
      }
      if ($country_code = apache_note('GEOIP_COUNTRY_CODE')) {
        $location['country_code'] = $country_code;
      }
      if ($region = apache_note('GEOIP_REGION_NAME')) {
        $location['region'] = $region;
      }
      if ($region_code = apache_note('GEOIP_REGION')) {
        $location['region_code'] = $region_code;
      }
      if ($city = apache_note('GEOIP_CITY')) {
        $location['city'] = $city;
      }
      if ($zip = apache_note('GEOIP_POSTAL_CODE')) {
        $location['zip'] = $zip;
      }
      if ($latitude = apache_note('GEOIP_LATITUDE')) {
        $location['latitude'] = $latitude;
      }
      if ($longitude = apache_note('GEOIP_LONGITUDE')) {
        $location['longitude'] = $longitude;
      }
    }
    else {
      if (isset($_SERVER['GEOIP_COUNTRY_NAME'])) {
        $location['country'] = $_SERVER['GEOIP_COUNTRY_NAME'];
      }
      elseif (isset($_SERVER['HTTP_X_GEOIP_COUNTRY'])) {
        module_load_include('inc', 'smart_ip', 'includes/smart_ip.country_list');
        $countries = country_get_predefined_list();
        $location['country'] = $countries[$_SERVER['HTTP_X_GEOIP_COUNTRY']];
      }
      elseif (isset($_SERVER['HTTP_CF_IPCOUNTRY'])) {
        module_load_include('inc', 'smart_ip', 'includes/smart_ip.country_list');
        $countries = country_get_predefined_list();
        $location['country'] = $countries[$_SERVER['HTTP_CF_IPCOUNTRY']];
      }
      if (isset($_SERVER['GEOIP_COUNTRY_CODE'])) {
        $location['country_code'] = $_SERVER['GEOIP_COUNTRY_CODE'];
      }
      elseif (isset($_SERVER['HTTP_X_GEOIP_COUNTRY'])) {
        $location['country_code'] = $_SERVER['HTTP_X_GEOIP_COUNTRY'];
      }
      elseif (isset($_SERVER['HTTP_CF_IPCOUNTRY'])) {
        $location['country_code'] = $_SERVER['HTTP_CF_IPCOUNTRY'];
      }
      if (isset($_SERVER['GEOIP_REGION_NAME'])) {
        $location['region'] = $_SERVER['GEOIP_REGION_NAME'];
      }
      if (isset($_SERVER['GEOIP_REGION'])) {
        $location['region_code'] = $_SERVER['GEOIP_REGION'];
      }
      if (isset($_SERVER['GEOIP_CITY'])) {
        $location['city'] = $_SERVER['GEOIP_CITY'];
      }
      if (isset($_SERVER['GEOIP_POSTAL_CODE'])) {
        $location['zip'] = $_SERVER['GEOIP_POSTAL_CODE'];
      }
      if (isset($_SERVER['GEOIP_LATITUDE'])) {
        $location['latitude'] = $_SERVER['GEOIP_LATITUDE'];
      }
      if (isset($_SERVER['GEOIP_LONGITUDE'])) {
        $location['longitude'] = $_SERVER['GEOIP_LONGITUDE'];
      }
    }
  }
}