Monk3h Posted January 4, 2011 Share Posted January 4, 2011 This script should detect what contry a visitor is from via there IP and display there contrys flag. However it does not work, please could you take a look and let me know if there are any obviouse errors as im stumped. <?php $IPaddress=$_SERVER['REMOTE_ADDR']; $two_letter_country_code=iptocountry($IPaddress); Print "$IPaddress" include("IP_FILES/countries.php"); $three_letter_country_code=$countries[ $two_letter_country_code][0]; $country_name=$countries[$two_letter_country_code][1]; print "Two letters code: $two_letter_country_code<br>"; print "Three letters code: $three_letter_country_code<br>"; print "Country name: $country_name<br>"; // To display flag $file_to_check="flags/$two_letter_country_code.gif"; if (file_exists($file_to_check)){ print "<img src=flags/$file_to_check width=30 height=15><br>"; }else{ print "<img src=flags/noflag.gif width=30 height=15><br>"; } function iptocountry($ip) { $numbers = preg_split( "/\./", $ip); include("ip_files/".$numbers[0].".php"); $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]); foreach($ranges as $key => $value){ if($key<=$code){ if($ranges[$key][0]>=$code){$two_letter_country_code=$ranges[$key][1];break;} } } if ($two_letter_country_code==""){$two_letter_country_code="unkown";} return $two_letter_country_code; } ?> Thanks in Advance, Monk3h. Link to comment https://forums.phpfreaks.com/topic/223375-ip-to-country-script/ Share on other sites More sharing options...
Kieran Menor Posted January 4, 2011 Share Posted January 4, 2011 Well, first of all, you are referring to the same directory (presumably) with two different casings (ip_files and IP_FILES). If the script is running on a non-Windows server, paths are case sensitive. Link to comment https://forums.phpfreaks.com/topic/223375-ip-to-country-script/#findComment-1154687 Share on other sites More sharing options...
Monk3h Posted January 4, 2011 Author Share Posted January 4, 2011 Nope, that was not it. I'm reletivly new to functions. Whats the most correct way to test the function? Link to comment https://forums.phpfreaks.com/topic/223375-ip-to-country-script/#findComment-1154692 Share on other sites More sharing options...
Kieran Menor Posted January 4, 2011 Share Posted January 4, 2011 I don't quite understand the logic of the function. Could you explain how it's supposed to work? Also, what do the files in ip_files contain? What do they look like? Link to comment https://forums.phpfreaks.com/topic/223375-ip-to-country-script/#findComment-1154695 Share on other sites More sharing options...
Monk3h Posted January 4, 2011 Author Share Posted January 4, 2011 an example of a file contained within ip_files is: <?php //4127195136-4143972351 $ranges=Array( "4127195136" => array("4143972351","ZZ"), ); ?> This is file 246.php These files are used as referances to define which ips match which countrys. Link to comment https://forums.phpfreaks.com/topic/223375-ip-to-country-script/#findComment-1154699 Share on other sites More sharing options...
Kieran Menor Posted January 4, 2011 Share Posted January 4, 2011 Try using explode('.', $ip) instead of preg_split(). The best way to test the function itself would be to copy it to a seperate file and simply feed it an IP to see what it returns. Link to comment https://forums.phpfreaks.com/topic/223375-ip-to-country-script/#findComment-1154716 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.