cayman_d Posted June 29, 2008 Share Posted June 29, 2008 Hi there. I'm trying to get a PHP script which will redirect visitors from an initial webpage (index.php - see below for code) to another webpage based on the country they are in (i.e. based on a range of IP addresses). The example I am working from is based on an external database of IP addresses, however since I am only concerned with redirecting traffic from one country (the Cayman Islands), I would rather just incorporate the IP address ranges into the PHP script. Any suggestions? <?php // Replace this MYSQL server variables with actual configuration $mysql_server = "123"; $mysql_user_name = "456"; $mysql_user_pass = "789"; // Retrieve visitor IP address from server variable REMOTE_ADDR $ipaddress = getenv(REMOTE_ADDR); // Convert IP address to IP number for querying database $ipno = Dot2LongIP($ipaddress); // Connect to the database server $link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database"); // Connect to the IP2Location database mysql_select_db("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 IPCountry WHERE $ipno <= ipTO AND $ipno>=ipFROM"; // Execute SQL query $result = mysql_query($query) or die("IP2Location Query Failed"); // Retrieve the recordset (only one) $row = mysql_fetch_object($result); // Keep the country information into two different variables $countrySHORT = $row->countrySHORT; $countryLONG = $row->countryLONG; // Free recordset and close database connection mysql_free_result($result); mysql_close($link); // If the visitors are from JP, redirect them to JP site if ($countrySHORT == "JP") { Header("Location: http://www.gov.ky"); } else { // Otherwise, redirect them to US site Header("Location: http://www.google.com"); } 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 = split ("\.", "$IPaddr"); return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256); } } ?> Link to comment https://forums.phpfreaks.com/topic/112506-solved-webpage-redirect-by-country-ip-range/ Share on other sites More sharing options...
br0ken Posted June 30, 2008 Share Posted June 30, 2008 Wouldn't it just be as simple as figuring out what the IP range is for the Cayman Islands then including a simple if statement to detect whether or not the current users IP address falls inside the Cayman Island's range? If it does, redirect, if not let them enter? Link to comment https://forums.phpfreaks.com/topic/112506-solved-webpage-redirect-by-country-ip-range/#findComment-577692 Share on other sites More sharing options...
cayman_d Posted June 30, 2008 Author Share Posted June 30, 2008 br0ken, That sounds like a great idea.I tried to cobble an If statement together but I am a PHP ignoramus and was hoping for some professional help. I figured the variable $ipaddress from the code below would be the starting point...then specify ranges of IP addresses to reject $ipaddress = getenv(REMOTE_ADDR); if ( $ipaddress > "63.136.112.0" or <"63.136.119.255" ) { REJECT"; else Header("Location: http://www.caymansite.ky"); } The IP ranges for Cayman are: 63.136.112.0 63.136.119.255 74.222.64.0 74.222.95.255 80.255.43.16 80.255.43.23 199.172.228.0 199.172.229.255 205.136.238.0 205.136.240.255 205.245.128.0 205.245.129.255 207.228.128.0 207.228.129.255 207.228.140.0 207.228.140.255 207.228.142.0 207.228.143.255 207.228.145.0 207.228.147.255 208.82.216.0 208.82.219.255 208.157.144.0 208.157.151.255 209.27.52.0 209.27.55.255 209.27.60.0 209.27.63.255 209.169.38.216 209.169.38.223 213.167.68.16 213.167.68.23 213.167.69.176 213.167.69.183 213.167.76.24 213.167.76.31 213.167.76.48 213.167.76.63 213.167.95.208 213.167.95.223 216.144.80.0 216.144.95.255 Link to comment https://forums.phpfreaks.com/topic/112506-solved-webpage-redirect-by-country-ip-range/#findComment-578202 Share on other sites More sharing options...
br0ken Posted June 30, 2008 Share Posted June 30, 2008 Give this a try, it should do what you want mate. I only added a sample of the IP addresses to the $buff variable so you add them all yourself in the following format: startIP,endIP;startIP,endIP; <?php $buff = "63.136.112.0,63.136.119.255;74.222.64.0,74.222.95.255;80.255.43.16,80.255.43.23;199.172.228.0,199.172.229.255;205.136.238.0,205.136.240.255"; $buff = explode(";", $buff); $ip = ip2long($_SERVER['REMOTE_ADDR']); foreach($buff as $range) { if (!$range) continue; $range = explode(",", $range); $min = ip2long($range[0]); $max = ip2long($range[1]); if ($ip >= $min && $ip<= $max) {// Ip in Given Range // Redirect User exit("User from The Claymans Island.");; } } ?> Link to comment https://forums.phpfreaks.com/topic/112506-solved-webpage-redirect-by-country-ip-range/#findComment-578275 Share on other sites More sharing options...
cayman_d Posted June 30, 2008 Author Share Posted June 30, 2008 br0ken, Thanks for this. I'm so close I can taste it! I want to use this as an initial sorting page (i.e. index.php) to route users to one page or another based on their country (i.e. Cayman Islands to one page, Rest of World to another). I tried to edit your script to insert a URL redirect instead of an exit but no luck...Any chance you can help push me over the final hurdle? <?php $buff = "63.136.112.0,63.136.119.255;74.222.64.0,74.222.95.255;80.255.43.16,80.255.43.23;199.172.228.0,199.172.229.255;205.136.238.0,205.136.240.255;205.245.128.0,205.245.129.255;207.228.128.0,207.228.129.255;207.228.140.0,207.228.140.255;207.228.142.0,207.228.143.255;207.228.145.0,207.228.147.255;208.82.216.0,208.82.219.255;208.157.144.0,208.157.151.255;209.27.52.0,209.27.55.255;209.27.60.0,209.27.63.255;209.169.38.216,209.169.38.223;213.167.68.16,213.167.68.23;213.167.69.176,213.167.69.183;213.167.76.24,213.167.76.31;213.167.76.48,213.167.76.63;213.167.95.208,213.167.95.223;216.144.80.0,216.144.95.255"; $buff = explode(";", $buff); $ip = ip2long($_SERVER['REMOTE_ADDR']); foreach($buff as $range) { if (!$range) continue; $range = explode(",", $range); $min = ip2long($range[0]); $max = ip2long($range[1]); if ($ip >= $min && $ip<= $max) {// Ip in Given Range // Redirect User exit("User from The Claymans Island.");; if ($ip < $min && $ip> $max) {// Ip outside Given Range // Redirect User header('Location: http://www.caymanislands.ky/');; } } ?> Link to comment https://forums.phpfreaks.com/topic/112506-solved-webpage-redirect-by-country-ip-range/#findComment-578363 Share on other sites More sharing options...
br0ken Posted June 30, 2008 Share Posted June 30, 2008 <?php $buff = "63.136.112.0,63.136.119.255;74.222.64.0,74.222.95.255;80.255.43.16,80.255.43.23;199.172.228.0,199.172.229.255;205.136.238.0,205.136.240.255;205.245.128.0,205.245.129.255;207.228.128.0,207.228.129.255;207.228.140.0,207.228.140.255;207.228.142.0,207.228.143.255;207.228.145.0,207.228.147.255;208.82.216.0,208.82.219.255;208.157.144.0,208.157.151.255;209.27.52.0,209.27.55.255;209.27.60.0,209.27.63.255;209.169.38.216,209.169.38.223;213.167.68.16,213.167.68.23;213.167.69.176,213.167.69.183;213.167.76.24,213.167.76.31;213.167.76.48,213.167.76.63;213.167.95.208,213.167.95.223;216.144.80.0,216.144.95.255"; $buff = explode(";", $buff); $ip = ip2long($_SERVER['REMOTE_ADDR']); foreach($buff as $range) { if (!$range) continue; $range = explode(",", $range); $min = ip2long($range[0]); $max = ip2long($range[1]); if ($ip >= $min && $ip<= $max) {// Ip in Given Range header('Location: http://www.caymanislands.ky/'); exit; } } // If code here, IP not from Cayman Islands header("Location: http://www.restofworld.com"); exit; ?> Link to comment https://forums.phpfreaks.com/topic/112506-solved-webpage-redirect-by-country-ip-range/#findComment-578475 Share on other sites More sharing options...
DarkWater Posted June 30, 2008 Share Posted June 30, 2008 Or you could just use the GeoIP functions, which does that stuff for you. Link to comment https://forums.phpfreaks.com/topic/112506-solved-webpage-redirect-by-country-ip-range/#findComment-578481 Share on other sites More sharing options...
cayman_d Posted June 30, 2008 Author Share Posted June 30, 2008 br0ken, thanks for all your help on this, looks like its working great. much obliged. Dark Water, I'll have a look at GeoIP on php.net and see if I cant figure out another solution. Thanks for the tip. Link to comment https://forums.phpfreaks.com/topic/112506-solved-webpage-redirect-by-country-ip-range/#findComment-578546 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.