Advertisement
hexasoft

Display Country Currency Using PHP and MySQL for IPv6

May 16th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.49 KB | None | 0 0
  1. /****************************************************************************************************************/
  2. /* Description: This snippet is provide simple way to help user display Visitor's Country Currency.             */
  3. /*              suggestions for different city locations across the country.                                    */
  4. /*              There are 1 steps in this snippet. For information, please visit IP2Location tutorial page at:  */
  5. /* https://www.ip2location.com/tutorials/display-visitors-country-currency-using-php-and-mysql-database         */
  6. /****************************************************************************************************************/
  7. /* You can obtain free IP Country database for IPv6 at https://lite.ip2location.com/database/ip-country         */
  8. /* You can obtain free country information data at https://www.ip2location.com/free/country-information         */
  9.  
  10. <?php
  11.     // Replace this MYSQL server variables with actual configuration
  12.     $mysql_server = "mysql_server.com";
  13.     $mysql_user_name = "UserName";
  14.     $mysql_user_pass = "Password";
  15.  
  16.     // Retrieve visitor IP address from server variable REMOTE_ADDR
  17.     $ipaddress = $_SERVER['REMOTE_ADDR'];
  18.  
  19.     // Convert IP address to IP number for querying database
  20.     $ipno = Dot2LongIPv6($ipaddress);
  21.      
  22.     // Connect to the database server
  23.     $link = mysqli_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
  24.  
  25.     // Connect to the IP2Location database
  26.     mysqli_select_db($link,"ip2location") or die("Could not select database");
  27.  
  28.     // SQL query string to match the recordset that the IP number fall between the valid range
  29.     $query = "SELECT * FROM ip_country WHERE $ipno <?= ip_to LIMIT 1";
  30.      
  31.     // Execute SQL query
  32.     $result = mysqli_query($link,$query) or die("IP2Location Query Failed");
  33.  
  34.     // Retrieve the recordset (only one)
  35.     $row = mysqli_fetch_assoc($result);
  36.  
  37.     // Keep the country information into two different variables
  38.     $country_code = $row['country_code'];
  39.     $country_name = $row['country_name'];
  40.  
  41.     // SQL query string to match the recordset that the tld with Country Short Name
  42.     $query = "SELECT * FROM ip2location_country_information WHERE country_code ='$country_code'";
  43.              
  44.     // Execute SQL query
  45.     $result = mysqli_query($link,$query) or die("IP2Location Query Failed");
  46.  
  47.     // Retrieve the recordset (only one)
  48.     $row = mysqli_fetch_assoc($result);
  49.  
  50.     // Keep the Currency name in a variable
  51.     $currency_name = $row['currency_name'];
  52.      
  53.     // Free recordset and close database connection
  54.     mysqli_free_result($result);
  55.     mysqli_close($link);
  56.  
  57.     // Display currency information
  58.     echo "The IP address : $ipaddress<?br/><?br/>";
  59.     echo "Visitor From : $country_name<?br/><?br/>";
  60.     echo "Your Currency is : $currency_name<?br/><?br/>";
  61.      
  62.     exit;
  63.  
  64.     // Function to convert IP address to IP number (IPv6)
  65.     function Dot2LongIPv6 ($IPaddr) {
  66.         $int = inet_pton($IPaddr);
  67.         $bits = 15;
  68.         $ipv6long = 0;
  69.         while($bits >= 0){
  70.             $bin = sprintf("%08b", (ord($int[$bits])));
  71.             if($ipv6long){
  72.                 $ipv6long = $bin . $ipv6long;
  73.             }
  74.             else{
  75.                 $ipv6long = $bin;
  76.             }
  77.             $bits--;
  78.         }
  79.         $ipv6long = gmp_strval(gmp_init($ipv6long, 2), 10);
  80.         return $ipv6long;                  
  81.     }
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement