Hi I have problem with script that shows "Warning: Invalid argument supplied for foreach() in /home/../../test.php on line 17". Script working, with showing this error on begining. Any idea what is the problem (since I am not programer, but I need that script ) PHP Version on my site is 4.4.3. Here is code: <? $IPaddress=$_SERVER['REMOTE_ADDR']; $two_letter_country_code=iptocountry($IPaddress); if ($two_letter_country_code=="UK"){ include("This_file_for_UK.html"); die(); }else{ include("This_file_for_others.html"); die(); } 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]); [B] foreach($ranges as $key => $value){[/B] 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; } ?> Code (markup): FYI, very nice script for geotargeting and country identification based on IP address and could be found here: phptutorial.info/iptocountry/the_script.html and here: phptutorial.info/iptocountry/responses.html thanks
change foreach($ranges as $key => $value){ to @foreach($ranges as $key => $value){ it's because not all of those files have items in, and so theres no data to loop over ..... the @ symbol supresses errors for that line
You may also check if $ranges is a array or not as, if(is_array($ranges) && count($ranges) > 0) { // your foreach loop goes here } PHP: