hexasoft icon

Display Country Currency Using PHP and MySQL for IPv4

hexasoft | PRO | 05/16/18 06:18:12 AM UTC | 0 ⭐ | 215 👁️ | Never ⏰ | []
PHP |

3.18 KB

|

None

|

0 👍

/

0 👎

/****************************************************************************************************************/
/* 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-mysql-database         */
/****************************************************************************************************************/
/* You can obtain free IP Country database for IPv4 at https://lite.ip2location.com/database/ip-country         */
/* You can obtain free country information data at https://www.ip2location.com/free/country-information         */
 
<?php
    // Replace this MYSQL server variables with actual configuration
    $mysql_server = "mysql_server.com";
    $mysql_user_name = "UserName";
    $mysql_user_pass = "Password";
 
    // Retrieve visitor IP address from server variable REMOTE_ADDR
    $ipaddress = $_SERVER['REMOTE_ADDR'];
 
    // Convert IP address to IP number for querying database
    $ipno = Dot2LongIP($ipaddress);
 
    // Connect to the database server
    $link = mysqli_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
 
    // Connect to the IP2Location database
    mysqli_select_db($link,"ip2location") or die("Could not select database");
 
    // SQL query string to match the recordset that the IP number fall between the valid range
    $query = "SELECT * FROM ip_country WHERE $ipno <= ip_to LIMIT 1";
 
    // Execute SQL query
    $result = mysqli_query($link,$query) or die("IP2Location Query Failed");
 
    // Retrieve the recordset (only one)
    $row = mysqli_fetch_assoc($result);
 
    // Keep the country information into two different variables
    $country_code = $row['country_code'];
    $country_name = $row['country_name'];
 
    // SQL query string to match the recordset that the tld with Country Short Name
    $query = "SELECT * FROM ip2location_country_information WHERE country_code ='$country_code'";
 
    // Execute SQL query
    $result = mysqli_query($link,$query) or die("IP2Location Query Failed");
 
    // Retrieve the recordset (only one)
    $row = mysqli_fetch_assoc($result);
 
    // Keep the Currency name in a variable
    $currency_name = $row['currency_name'];
 
    // Free recordset and close database connection
    mysqli_free_result($result); 
    mysqli_close($link);
 
    // Display currency information
        echo "The IP address : $ipaddress<br/><br/>";
        echo "Visitor From : $country_name<br/><br/>";
        echo "Your Currency is : $currency_name<br/><br/>";       
        exit;
 
    // Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to 256^4-1)
    function Dot2LongIP ($IPaddr) {
     if ($IPaddr == "")
     {
       return 0;
     } else {
       $ips = explode(".", $IPaddr);
       return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
     }
    } 
?>

Comments