Advertisement
hexasoft

Display Advertisement by Country Used PHP and MySQL for IPV6

May 16th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.83 KB | None | 0 0
  1. /****************************************************************************************************************/
  2. /* Description: This snippet is provide simple way to help user display Advertisement by Country                */
  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-advertisement-by-country-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.  
  9. <?php
  10.     // Replace this MYSQL server variables with actual configuration
  11.     $mysql_server = "mysql_server.com";
  12.     $mysql_user_name = "UserName";
  13.     $mysql_user_pass = "Password";
  14.  
  15.     // Retrieve visitor IP address from server variable REMOTE_ADDR
  16.     $ipaddress = $_SERVER['REMOTE_ADDR'];
  17.  
  18.     // Convert IP address to IP number for querying database
  19.     $ipno = Dot2LongIPv6($ipaddress);
  20.  
  21.     // Connect to the database server
  22.     $link = mysqli_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
  23.      
  24.     // Connect to the database server
  25.     mysqli_select_db($link,"ip2location") or die("Could not select database");
  26.  
  27.     // SQL query string to match the recordset that the IP number fall between the valid range
  28.     $query = "SELECT * FROM ip_country WHERE $ipno <= ip_to LIMIT 1";
  29.  
  30.     // Execute SQL query
  31.     $result = mysqli_query($link,$query) or die("IP2Location Query Failed");
  32.  
  33.     // Retrieve the recordset (only one)
  34.     $row = mysqli_fetch_assoc($result);
  35.  
  36.     // Keep the country information into two different variables
  37.     $country_code = $row['country_code'];
  38.     $country_name = $row['country_name'];
  39.  
  40.     // Free recordset and close database connection
  41.     mysqli_free_result($result);
  42.     mysqli_close($link);
  43.      
  44.     // Display ads according to visitor's country,replace this to your own code
  45.     echo 'This is the advertisement for' . $country_name .'<br/><br/>';
  46.      
  47.     exit;
  48.      
  49.  
  50.     // Function to convert IP address to IP number (IPv6)
  51.     function Dot2LongIPv6 ($IPaddr) {
  52.         $int = inet_pton($IPaddr);
  53.         $bits = 15;
  54.         $ipv6long = 0;
  55.         while($bits >= 0){
  56.             $bin = sprintf("%08b", (ord($int[$bits])));
  57.             if($ipv6long){
  58.                 $ipv6long = $bin . $ipv6long;
  59.             }
  60.             else{
  61.                 $ipv6long = $bin;
  62.             }
  63.             $bits--;
  64.         }
  65.         $ipv6long = gmp_strval(gmp_init($ipv6long, 2), 10);
  66.         return $ipv6long;
  67.     }
  68. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement