/** * Resolves an IP address to a country code * * @param string $ipaddr IP address to resolve (defaults to <get_ip_address>) * @return array Country code or empty string if not found */ function get_countrycode_by_ip($ipaddr = false) { if ($ipaddr === false) { $ipaddr = $GLOBALS['current_ip_addr']; } if (isset($_SESSION['geoip_countrycode_by_ip_' . $ipaddr]) && $_SESSION['geoip_countrycode_by_ip_' . $ipaddr] != "") { return $_SESSION['geoip_countrycode_by_ip_' . $ipaddr]; } if (function_exists('geoip_open')) { $gi = geoip_open($GLOBALS['CONFIG']['geoip']['city_dat_file'], GEOIP_STANDARD); $country_code = geoip_country_code_by_addr($gi, $ipaddr); geoip_close($gi); } else { $country_code = geoip_country_code_by_name($ipaddr); } if ($country_code == "") { if (isDev() && starts_with($ipaddr, '192.168.')) { $country_code = 'DE'; } else { $location = get_geo_location_by_ip($ipaddr); if ($location && isset($location->country_code)) { $country_code = $location->country_code; } } } $_SESSION['geoip_countrycode_by_ip_' . $ipaddr] = $country_code . ""; return $country_code; }
/** * Resolves an IP address to a country code * * @param string $ipaddr IP address to resolve (defaults to <get_ip_address>) * @return array Country code or empty string if not found */ function get_countrycode_by_ip($ipaddr = false) { if ($ipaddr === false) { $ipaddr = $GLOBALS['current_ip_addr']; } if (isset($_SESSION['geoip_countrycode_by_ip_' . $ipaddr]) && $_SESSION['geoip_countrycode_by_ip_' . $ipaddr] != "") { return $_SESSION['geoip_countrycode_by_ip_' . $ipaddr]; } // // maxmind installed as server module? // if(isset($_SERVER["GEOIP_COUNTRY_CODE"])) // return $_SERVER["GEOIP_COUNTRY_CODE"]; if (function_exists('geoip_open')) { $gi = geoip_open($GLOBALS['CONFIG']['geoip']['city_dat_file'], GEOIP_STANDARD); $country_code = geoip_country_code_by_addr($gi, $ipaddr); // log_debug("country: ".$country_code); geoip_close($gi); } else { $country_code = geoip_country_code_by_name($ipaddr); } if ($country_code == "") { if (isDev() && starts_with($ipaddr, '192.168.1.')) { $country_code = 'DE'; } else { $location = get_geo_location_by_ip($ipaddr); if ($location && isset($location->country_code)) { $country_code = $location->country_code; } } } $_SESSION['geoip_countrycode_by_ip_' . $ipaddr] = $country_code . ""; return $country_code; }