Undefined variable:

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
drgl1966
New php-forum User
New php-forum User
Posts: 5
Joined: Thu Nov 28, 2019 9:13 am

Thu Nov 28, 2019 9:20 am

Hi guys, hope someone can help? I noticed an error in my logs & upon digging it seems this script is causing the issue:-

Code: Select all

<?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!

Thanks in advance for any help with this...
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 864
Joined: Mon Feb 22, 2016 5:52 pm

Thu Nov 28, 2019 11:27 am

I can't be certain as I don't know if $two_letter_country_code is defined in one of your includes?

Code: Select all

include("ip_files/".$numbers[0].".php");
Anyway, try declaring it in your function call (it's considered a good idea to declare variables before first use):

Code: Select all

function iptocountry($ip) {
    $two_letter_country_code = "";
    .....
drgl1966
New php-forum User
New php-forum User
Posts: 5
Joined: Thu Nov 28, 2019 9:13 am

Thu Nov 28, 2019 1:01 pm

Thanks. I'll give it a go in the morning :)
drgl1966
New php-forum User
New php-forum User
Posts: 5
Joined: Thu Nov 28, 2019 9:13 am

Fri Nov 29, 2019 2:01 am

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

Code: Select all

"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 :)
drgl1966
New php-forum User
New php-forum User
Posts: 5
Joined: Thu Nov 28, 2019 9:13 am

Fri Dec 06, 2019 8:22 am

Anyone? Here's the original authors site again :-


http://phptutorial.info/iptocountry/the_script.html
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 864
Joined: Mon Feb 22, 2016 5:52 pm

Mon Dec 09, 2019 10:20 am

Probably because if there is no file, it uses 'unknown.php' as the filename?
drgl1966
New php-forum User
New php-forum User
Posts: 5
Joined: Thu Nov 28, 2019 9:13 am

Tue Dec 10, 2019 9:37 am

But where is that referenced in the code? I can't see where it is ever used? There's an include for countries.php but nothing for unknown.
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 864
Joined: Mon Feb 22, 2016 5:52 pm

Tue Dec 10, 2019 10:28 am

Probably because if there is no file, it uses 'unknown.php' as the filename?
I don't know where in the code it will be as I don't have all the code, I'm just guessing. It may have been something that the original author did at one time and hasn't removed it.

I haven't looked at the link you provided and can see no reason to do so.
Post Reply