Advertisement
hexasoft

Redirect Visitors By Country Using PHP and MySQL for IPV4

May 16th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.64 KB | None | 0 0
  1. /****************************************************************************************************************/
  2. /* Description: This snippet is provide simple way to redirect visitor to different default web page based on   */
  3. /*              visitor's country of origin                                                                     */
  4. /*              suggestions for different city locations across the country.                                    */
  5. /*              There are 1 steps in this snippet. For information, please visit IP2Location tutorial page at:  */
  6. /* https://www.ip2location.com/tutorials/redirect-web-visitors-by-country-using-php-and-mysql-database          */
  7. /****************************************************************************************************************/
  8. /* You can obtain free IP Country database for IPv4 at https://lite.ip2location.com/database/ip-country         */
  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 = Dot2LongIP($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.      
  42.     // Free recordset and close database connection
  43.     mysqli_free_result($result);
  44.     mysqli_close($link);
  45.      
  46.  
  47.     // Redirect user to the correct page
  48.     echo 'You will be redirected to ' . $country_code . ' page <br/><br/>';
  49.     exit;
  50.  
  51.     // Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to 256^4-1)
  52.     function Dot2LongIP ($IPaddr) {
  53.      if ($IPaddr == "")
  54.      {
  55.        return 0;
  56.      } else {
  57.        $ips = explode(".", $IPaddr);
  58.        return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
  59.      }
  60.     }
  61. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement