Ebrahim Khalil Ebrahim Khalil - 1 month ago 15
PHP Question

GEO IP can't run in Codeigniter Framework

I'm facing a problem, if-else not working. Here is code.

$ip = $_SERVER['REMOTE_ADDR']; //ip is - 182.48.65.4
$adsmanager = file_get_contents("http://ipinfo.io/$ip/country");
//adsmanager result: BD
if($adsmanager == "BD"){
echo '1. This is Bangladesh';
} else {
echo '2. This is USA';
}


But After run Output is "2. This is USA"

again when is change

if($adsmanager == "US"){
echo '1. This is Bangladesh';
} else {
echo '2. This is USA';
}


And Output is "2. This is USA"

What is the problem? I don't understand.

Answer

Use trim() function ...then your match will work. Because the page returns a linebreak with output.

if(trim($adsmanager) == "BD"){
     echo '1. This is Bangladesh';
} else {
     echo '2. This is USA';
}