ohno Posted November 28, 2019 Share Posted November 28, 2019 Hi guys, hope someone can help? I noticed an error in my logs & upon digging it seems this script is causing the issue:- <?php error_reporting(E_ALL); //$IPaddress=$_SERVER['REMOTE_ADDR']; $IPaddress='45.152.180.38'; $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]; 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=$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="UNKNOWN";} return $two_letter_country_code; } ?> This script was originally from here http://phptutorial.info/iptocountry/the_script.html The error I'm getting is :- [28-Nov-2019 16:57:56 UTC] PHP Notice: Undefined variable: two_letter_country_code in /home/drgl1966/testing.site/testinguser/test.php on line 35 I'm ***guessing*** this may be caused by my newer version of PHP (7.3, although I must admit I didn't look at old logs to see if the problem exists with older PHP version). I hard coded the made up IP in the code above as the problem only happens if the IP address is not known. Any idea's how to fix? As you can probably gather I'm not a PHP coder! I've searched online for answers but all the things I've found & tried have failed Thanks in advance for any help with this... Quote Link to comment https://forums.phpfreaks.com/topic/309591-undefined-variable/ Share on other sites More sharing options...
Barand Posted November 28, 2019 Share Posted November 28, 2019 (edited) Nothing to do with version of PHP. The problem is with variable scope. You need to pass the variable to the function, same as you have with $ip. foreach($ranges as $key => $value){ if($key<=$code){ if($ranges[$key][0]>=$code){$two_letter_country_code=$ranges[$key][1];break;} } } On closer reading, the variable is defined in the loop, but only if the two conditions are true. Otherwise it is undefined. Define it as $two_letter_country_code = ''; before the foreach loop; Edited November 28, 2019 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/309591-undefined-variable/#findComment-1571974 Share on other sites More sharing options...
ohno Posted November 28, 2019 Author Share Posted November 28, 2019 Thanks. I'll give that a go in the morning Quote Link to comment https://forums.phpfreaks.com/topic/309591-undefined-variable/#findComment-1571976 Share on other sites More sharing options...
maxxd Posted November 29, 2019 Share Posted November 29, 2019 I can't see where $ranges is defined, so once you define $two_letter_country_code you may have another undefined variable error unless it's defined in "ip_files/".$numbers[0].".php". Quote Link to comment https://forums.phpfreaks.com/topic/309591-undefined-variable/#findComment-1571977 Share on other sites More sharing options...
ohno Posted November 29, 2019 Author Share Posted November 29, 2019 Fixed I was on the right tracks but had put the code in the wrong place, oops. Anyway, many thanks for helping to sort it. On a side note the script came with a file "unknown.php" which is just empty, I can't see how the code would ever use the file as I can't see it referenced in the code anywhere? Anyway, what I did was modify "countries.php" by adding this code :- "UNKNOWN" => array("Undeclared","an undeclared location"), It all works as it should, if the IP is from an unknown location the text added to countries.php is displayed. It just seems odd the original author had "unknown.php"? Thanks again for your valued help Quote Link to comment https://forums.phpfreaks.com/topic/309591-undefined-variable/#findComment-1571985 Share on other sites More sharing options...
ohno Posted December 6, 2019 Author Share Posted December 6, 2019 Anyone? Here's the original authors site again :- http://phptutorial.info/iptocountry/the_script.html Quote Link to comment https://forums.phpfreaks.com/topic/309591-undefined-variable/#findComment-1572297 Share on other sites More sharing options...
maxxd Posted December 7, 2019 Share Posted December 7, 2019 9 hours ago, ohno said: Anyone? What exactly do you need? You said you fixed it... Quote Link to comment https://forums.phpfreaks.com/topic/309591-undefined-variable/#findComment-1572342 Share on other sites More sharing options...
ohno Posted December 9, 2019 Author Share Posted December 9, 2019 As above, the script came with a file "unknown.php" which is just empty, I can't see how the code would ever use the file as I can't see it referenced in the code anywhere? What I did was modify "countries.php" by adding this code :- "UNKNOWN" => array("Undeclared","an undeclared location"), It all works as it should, if the IP is from an unknown location the text I added to countries.php is displayed. It just seems odd the original author had "unknown.php"? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/309591-undefined-variable/#findComment-1572391 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.