'USD', 'US' => 'USD', 'UK' => 'GBP', 'PL' => 'PLN', 'DE' => 'EUR', 'NL' => 'EUR', 'FR' => 'EUR', 'IT' => 'EUR', 'CH' => 'CHF', // NOTE: You can add more below ); $country_to_language = array( 'default' => 'english', 'US' => 'english', 'UK' => 'english', 'DE' => 'german', 'FR' => 'french', 'IT' => 'italian', // NOTE: You can add more below ); $allowed_scripts = array( 'index.php', 'clientarea.php', 'cart.php', 'knowledgebase.php', 'announcements.php', 'serverstatus.php', 'affiliates.php', 'contact.php' // NOTE: You can add more below ); /** * FUNCTION geolocation_getCurrencyId * This function will return currency id for specific code. * * @param string * @return int */ function geolocation_getCurrencyId($currency) { $q = mysql_query('SELECT id FROM tblcurrencies WHERE code = "'.mysql_escape_string($currency).'"'); // escape string just in case $r = mysql_fetch_assoc($q); mysql_free_result($q); if(isset($r['id'])) { return $r['id']; } } /** * Main Geolocation Script * * NOT run script * - if we are in adminarea * - if already setup for this session * - if user is logged in * - NEW: allowing to run the hook only for specific scripts (defined above) */ $_script_path = substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], DIRECTORY_SEPARATOR)+1); if(in_array($_script_path, $allowed_scripts) && strpos($_SERVER['REQUEST_URI'], $customadminpath) === false && !isset($_SESSION['geolocation_setup']) && $_SESSION['uid'] == false) { $_SESSION['geolocation_setup'] = true; // prevent from redirecting back again in this session /** * Get Country using external service - Hostip.info example * NOTE: You can handle this part with any other method. */ $current_country = ''; $ret = file_get_contents("http://api.hostip.info/get_json.php?ip=".$_SERVER['REMOTE_ADDR']); // this can be called also via cURL $parsed_json = @json_decode($ret,true); if(isset($parsed_json['country_code'])) { $current_country = $parsed_json['country_code']; } /** * Get language, currency and currency ID in order to setup the right values in the system */ $currency = $current_country != '' && isset($country_to_currency[$current_country]) ? $country_to_currency[$current_country] : $country_to_currency['default']; $currency_id = geolocation_getCurrencyId($currency); $language = $current_country != '' && isset($country_to_language[$current_country]) ? $country_to_language[$current_country] : $country_to_language['default']; /** * Setup Currency Session * NOTE: You can remove/disable this part if not needed. */ if($currency_id && (!isset($_SESSION['currency']) || $_SESSION['currency'] != $currency)) { $_SESSION['currency'] = $_SESSION['switched_currency'] = $currency_id; } /** * Setup URL Redirection to Switch Language * NOTE: You can remove/disable this part if not needed. */ if(!isset($_SESSION['Language']) || $_SESSION['Language'] != $language) { $location = '?language='.$language; if($_SERVER['QUERY_STRING'] != '') $location .= '&'.$_SERVER['QUERY_STRING']; ob_clean(); header('location: '.$location); die(); } } /** * Preventing from switching currency by user * NOTE: You can remove/disable this part if not needed. */ if(isset($_SESSION['switched_currency']) && $_SESSION['switched_currency'] != $_SESSION['currency'] ) { $_SESSION['currency'] = $_SESSION['switched_currency']; } ?>