Jump to content

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/309591-undefined-variable/
Share on other sites

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 by Barand
Link to comment
https://forums.phpfreaks.com/topic/309591-undefined-variable/#findComment-1571974
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/309591-undefined-variable/#findComment-1571985
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/309591-undefined-variable/#findComment-1572391
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.