Page 1 of 1

ip-to-country dropdown!

Posted: Wed Mar 23, 2005 2:00 pm
by Joe
I have been making an ip-to-country script which works perfect, however when I try to make a dropdown list with the appropiate country selection it fails to work. My code is as follows:

Code: Select all

<?php
include("Website/includes/ip2addr.php");

$contents = array('AFGHANISTAN','ALBANIA','ALGERIA','AMERICAN SAMOA','ANDORRA','ANGOLA','ANGUILLA','ANTARCTICA','ANTIGUA AND BARBUDA','ARGENTINA','ARMENIA','ARUBA','AUSTRALIA','AUSTRIA','AZERBAIJAN','BAHAMAS','STATE OF BAHRAIN','BANGLADESH','BARBADOS','BELARUS','BELGIUM','BELIZE','BENIN','BERMUDA','BHUTAN','BOLIVIA','BOSNIA AND HERZEGOWINA','BOTSWANA','BOUVET ISLAND','BRAZIL','BRUNEI DARUSSALAM','BULGARIA','BURKINA FASO','BURUNDI','CAMBODIA','CAMEROON','CANADA','CAPE VERDE','CAYMAN ISLANDS','CENTRAL AFRICAN REPUBLIC','CHAD','CHILE','CHINA','CHRISTMAS ISLAND','COCOS (KEELING) ISLANDS','COLOMBIA','COMOROS','CONGO', 'COOK ISLANDS','COSTA RICA','CUBA','CYPRUS','CZECH REPUBLIC','DENMARK','DJIBOUTI','DOMINICA','DOMINICAN REPUBLIC','EAST TIMOR','ECUADOR','EGYPT','EL SALVADOR','EQUATORIAL GUINEA','ERITREA','ESTONIA','ETHIOPIA','FAROE ISLANDS','FIJI','FINLAND','FRANCE','FRENCH GUIANA','FRENCH POLYNESIA','GABON','GAMBIA','GEORGIA','GERMANY','GHANA','GIBRALTAR','GREECE','GREENLAND','GRENADA','GUADELOUPE','GUAM','GUATEMALA','GUINEA','GUINEA-BISSAU','GUYANA','HAITI','HONDURAS','HONG KONG','HUNGARY','ICELAND','INDIA','INDONESIA','IRAN','IRAQ','IRELAND','ISRAEL','ITALY','JAMAICA','JAPAN','JORDAN','KAZAKHSTAN','KENYA','KIRIBATI','KOREA','KUWAIT','KYRGYZSTAN','LATVIA','LEBANON','LESOTHO','LIBERIA','LIECHTENSTEIN','LITHUANIA','LUXEMBOURG','MACAU','MADAGASCAR','MALAWI','MALAYSIA','MALDIVES','MALI','MALTA','MARSHALL ISLANDS','MARTINIQUE','MAURITIUS','MAYOTTE','MEXICO','MONACO','MONGOLIA','MONTSERRAT','MOROCCO','MOZAMBIQUE','MYANMAR','NAMIBIA','NAURU','NEPAL','NETHERLANDS','NETHERLANDS ANTILLES','NEW CALEDONIA','NEW ZEALAND','NICARAGUA','NIGER','NIGERIA','NIUE','NORFOLK ISLAND','NORWAY','PAKISTAN','PALAU','PANAMA','PAPUA NEW GUINEA','PARAGUAY','PERU','PHILIPPINES','PITCAIRN','POLAND', 'PUERTO RICO','QATAR','REUNION','ROMANIA','RUSSIAN FEDERATION','RWANDA','SAMOA','SAN MARINO','SAUDI ARABIA','SENEGAL','SEYCHELLES','SIERRA LEONE','SINGAPORE','SLOVAKIA','SLOVENIA','SOMALIA','SOUTH AFRICA','SPAIN','SRI LANKA','SUDAN','SURINAME','SWAZILAND','SWEDEN','SWITZERLAND','TAIWAN','TAJIKISTAN','THAILAND','TONGA','TUNISIA','TURKEY','TURKMENISTAN','TUVALU','UGANDA','UKRAINE','UNITED KINGDOM','UNITED STATES','URUGUAY','UZBEKISTAN','VANUATU','VENEZUELA','VIET NAM','VIRGIN ISLANDS','WESTERN SAHARA','YEMEN','YUGOSLAVIA','ZAIRE','ZAMBIA','ZIMBABWE','EUROPE');

$user_country = IP_to_Country($_SERVER['REMOTE_ADDR']);

echo '<select class="dropdown" name="country">';


for($i=0; $i<count($contents); $i++) {

 if (strtoupper($contents[$i]) == strtoupper($user_country)) {
   echo '<option value="'. $contents[$i] .'" selected>'. $contents[$i] .'</option>';  
 } 
 elseif (strtoupper($contents[$i]) != strtoupper($user_country)) {
   echo '<option value="'. $contents[$i] .'">'. $contents[$i] .'</option>';
 }
}

echo '</select>';
?>
If I echo the $user_country variable I am resulted in UNITED KINGDOM so there must be a fix.

Thanks


Joe :)

Re: ip-to-country dropdown!

Posted: Wed Mar 23, 2005 2:08 pm
by John Cartwright
no need for the 2nd if.. just make it an else
and no need for the first strtoupper

Code: Select all

for($i=0; $i<count($contents); $i++) {

 if ($contents[$i] == strtoupper($user_country)) {
   echo '<option value="'. $contents[$i] .'" selected>'. $contents[$i] .'</option>';  
 } 
 else {
   echo '<option value="'. $contents[$i] .'">'. $contents[$i] .'</option>';
 }

}
Try echo'ing out your variables in the loop, to see where it fails.

Code: Select all

for($i=0; $i<count($contents); $i++) {

 if ($contents[$i] == strtoupper($user_country)) {
   echo '<option value="'. $contents[$i] .'" selected>'. $contents[$i] .'</option>';  
 } 
 else {
   echo '<option value="'. $contents[$i] .'">'. $contents[$i] .'</option>';
 }

 echo $contents[$i].'--'.$user_country;
}
I dont see anything that is jumping out and biting me..

Posted: Wed Mar 23, 2005 2:24 pm
by Joe
I tried that out but I do not see any problems and it still fails to work :(

Posted: Wed Mar 23, 2005 2:34 pm
by Roja
its fairly challenging for us to recreate the issue without the ip2addr.php file. :)

Posted: Wed Mar 23, 2005 2:54 pm
by Joe
Roja wrote:its fairly challenging for us to recreate the issue without the ip2addr.php file. :)
Not really. I told you that if I echo out the $user_country variable it displays the country where the user is based. Here is the code anyway:

Code: Select all

<?php
function IP_to_Country($ip) {
 $numbers=explode (".",$ip);
 $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);    
 $lis="0";
 $user = file("includes/ips.dat");

 for($x=0;$x<sizeof($user);$x++) {
  $temp = explode(";",$user[$x]);
  $opp[$x] = "$temp[0];$temp[1];$temp[2];$temp[3];$temp[4];";

   if($code >= $temp[0] && $code <= $temp[1]) {
     $list[$lis] = $opp[$x];
     $lis++; 
    }
   }

 if(sizeof($list) != "0") {
  for ($i=0; $i<sizeof($list); $i++){
   $p=explode(';', $list[$i]);
   return strtoupper($p[4]);
  }
}
else {
 echo "Unable to determine your country"; 
 }
}
?>
The ips.dat file is very large so I do not want to post it.

Posted: Wed Mar 23, 2005 3:20 pm
by Roja
Joe wrote: Not really. I told you that if I echo out the $user_country variable it displays the country where the user is based. Here is the code anyway:

The ips.dat file is very large so I do not want to post it.
You misunderstand. Explaining the behavior does not change the inability for us to run the same thing, and get the same results, and recode to figure out what the cause is.

Just *looking* at the sample code, and the description, the problem comes down to:

Code: Select all

if (strtoupper($contents[$i]) == strtoupper($user_country))
Something about $user_country is NOT the same as the array entry. I presume (again, cant test!) that NO country is being "selected" in the dropdown, because none are matching.

So, I suggest doing a var_dump on $user_country, and doing the same for all elements of $contents[$i] to see what the difference is.. it may be a trailing space, spelling, anything. *something* is different.

Thats why it helps to have the code to recreate the issue: So we can work WITH you, instead of guessing and presuming.

Posted: Wed Mar 23, 2005 3:23 pm
by Joe
No worries mate I solved the problem. The ips.dat file was not being read properly and it was causing some distruption. Thanks anyway!.

Posted: Wed Mar 23, 2005 3:25 pm
by Roja
Joe wrote:No worries mate I solved the problem. The ips.dat file was not being read properly and it was causing some distruption. Thanks anyway!.
Wait - So you mean the one thing you DIDNT include for us to be able to test caused the very problem you asked us to solve?

Thanks for making my point for me.

Posted: Wed Mar 23, 2005 3:33 pm
by Joe
lol mmkay. Posting that particular file would have taken ages to tranfer. It is very very very LARGE!!!. But yes your right.

Posted: Wed Mar 23, 2005 7:18 pm
by Pyrite
I assume you are using the iptocountry.csv ? If so, that is only 2+ MB . If not, what ip dat are you using?

Posted: Wed Mar 23, 2005 7:30 pm
by Roja
For large files, dont post em, link to em. Host them somewhere, and link to them. :)

Posted: Wed Mar 23, 2005 7:36 pm
by Ambush Commander
You could have also posted a small sample of the data information so that we could have seen what would have happened.