/****************************************************************************************************************/
/* Description: This snippet is provide simple way to help user display Visitor's Country Currency. */
/* suggestions for different city locations across the country. */
/* There are 1 steps in this snippet. For information, please visit IP2Location tutorial page at: */
/* https://www.ip2location.com/tutorials/display-visitors-country-currency-using-php-and-bin-database */
/****************************************************************************************************************/
/* You can obtain free IP2Location LITE database for bin at https:https://lite.ip2location.com/ */
/* You can obtain free IP2Location PHP class module data at https://www.ip2location.com/developers/php */
<?php
// Preset PHP settings
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(0);
require_once('IP2Location.php');
// Standard lookup with no cache
$loc = new \IP2Location\Database('./databases/IP2LOCATION-LITE-DB1.BIN', \IP2Location\Database::FILE_IO);
$ip = $_SERVER['REMOTE_ADDR'];
//lookup for country code
$country_code = $loc->lookup($ip, \IP2Location\Database::COUNTRY_CODE);
//Lookup and display country code and country name
echo "Country Code: " . $loc->lookup($ip, \IP2Location\Database::COUNTRY_CODE) . "<br />";
echo "Country Name: " . $loc->lookup($ip, \IP2Location\Database::COUNTRY_NAME) . "<br />";
//Displaying currency code based on country code retrieved.
//If no record of currency code or no country code is found, default will be used.
switch($country_code)
{
case 'CA':
$currency_code = "CAD";
echo "Country Currency: $currency_code 100";
break;
case 'MY':
$currency_code = "MYR";
echo "Country Currency: $currency_code 100";
break;
case 'JP':
$currency_code = "JPY";
echo "Country Currency: $currency_code 100";
break;
case 'US':
$currency_code = "USD";
echo "Country Currency: $currency_code 100";
break;
default:
echo "Unable to translate IP to country. <br />";
echo "$100.00";
break;
}
?>
Comments