tags:

views:

24

answers:

1

Hello, I am trying to display a country flag based on the user's IP address. Previously, I had it working fine, but I think I accidentally did something wrong. After hours of debugging, I can't seem to find what's causing the error. It's not throwing any errors, but its simply not displaying any flags or any of the 'echo' statements. Does anyone have any advice? The code is below, thanks in advance.

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="unknown";}  
return $two_letter_country_code;  
$IPaddress=$_SERVER['REMOTE_ADDR'];  
$two_letter_country_code=iptocountry($IPaddress);  

include("ip_files/countries.php");  
$three_letter_country_code=$countries[ $two_letter_country_code][0];  
$country_name=$countries[$two_letter_country_code][1];  

echo "Two letters code: $two_letter_country_code<br>";  
echo "Three letters code: $three_letter_country_code<br>";  
echo "Country name: $country_name<br>";  

// To display flag  
$file_to_check="flags/$two_letter_country_code.gif";  
if (file_exists($file_to_check)){  
            echo `"<img src=$file_to_check width=30 height=15><br>"`;  
            }else{  
            echo `"<img src=flags/noflag.gif width=30 height=15><br>"`;  
            }  


}  
?>  
+3  A: 

you're missing a closing brace } after your functions return statement.

Also, your code is extremely tight and cramped. Have you ever considered a more spacious coding style? It really helps on the eyes, and it would probably help you find bugs like this more quickly.

jordanstephens
+1 for suggesting an improved coding style
George Marian
Thank you so much, silly mistake, and I'll definitely take that into consideration.
Newbie_25