Jump to content
ohno

Undefined variable:

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites

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.