I have a mysql database to find the country of the IP address. Is it possible to get the country without connecting to/querying the database?
No. There is no relationship between the IP address and the country.
If you wanted to put all the data in an array, then that would let you not have to connect to the database. But that would cause the file to be huge.
You're basically asking: I have a car, is it possible to drive the car somewhere without starting or steering it?
If you have a database, use it. They're there to facilitate retrieving/sorting data. Anything else is likely to be slower/less reliable.
If you're dead set on it, then dump the database into a file and load/parse it for every request your search script processes.
You could use a 3rd party API.
One of the first ones that comes up in Google:
Example: http://ip2country.hackers.lv/api/ip2country?ip=11.12.13.14
Hmmm... I see answers "It's impossible", so just to show that it is possible and it will work almost fast as DB, I'll put this php-class:
http://www.filefactory.com/file/cee5cb6/n/sypexgeo.zip
It's not my code, you'll see authors in comments. This library is not supported anymore by authors and some IP can't be defined. If you will put content of file SypexGeo.dat into cache (322Kb), it will work faster, than DB, but in case with DB you can easily update list of new IP's.
If you don't want to use a Database, you could use gethostbyaddr
:
$country = substr(strrchr(gethostbyaddr($_SERVER['REMOTE_ADDR']), '.'), 1);
Here $country
may contain values like de
or uk
, but often this value is something like com
or net
which doesn't represent a country.
Additionally gethostbyaddr
may be slow..
So I would use a Database.
this class use text based database you can use it
http://www.phpclasses.org/package/2363-PHP-Lookup-the-country-of-a-given-IP-address.html
or here this one use cvs database
http://www.phpclasses.org/package/1477-PHP-Determine-the-country-of-a-given-IP-address.html
Here is the solution I'm using on my site www.vladonai.com, it's 100% free (using daily updated www.zaigadgets.com/geoip source), very fast (possibly much faster than MySQL could do), and requires no database. The database is stored in a binary file, so all what you need is any version of PHP.
The function to convert IP to Country name looks like this:
function GetCountryFromIP($ip = "")
{
if ($ip == "")
$ip = $_SERVER['REMOTE_ADDR'];
//the db obtained from ZaiGadgets.com and converted using convert_ip2country_db_from_csv_to_binary_file_format function
$db_file_path = ip2Country_binary_db_filename;
if (!file_exists($db_file_path))
return "";
$db_file_handle = fopen($db_file_path, "rb");
if (!$db_file_handle)
return "";
$one_record_size = 10; //bytes
$low = 0;
$high = (filesize($db_file_path) / $one_record_size) - 1;
$mid = 0;
$ipFrom = 0;
$ipTo = 0;
$ipArrParts = explode('.', $ip);
$ipLong = ($ipArrParts[0] * 0x1000000) + ($ipArrParts[1] * 0x10000) + ($ipArrParts[2] * 0x100) + ($ipArrParts[3]);
$country = ""; //result
while($low <= $high)
{
$mid = (int)(($low + $high) / 2);
$file_pos = $mid * $one_record_size;
fseek($db_file_handle, $file_pos, SEEK_SET);
$ipFrom = ipdbv2_read32($db_file_handle);
$ipTo = ipdbv2_read32($db_file_handle);
if ($ipFrom < 0)
$ipFrom += pow(2, 32);
if ($ipTo < 0)
$ipTo += pow(2, 32);
if (($ipLong >= $ipFrom) && ($ipLong < $ipTo))
{
//found IP range :)
//echo "Found!!!!!<br>";
$country = fread($db_file_handle, 2);
fclose($db_file_handle);
$country = GetCountryNameFromCountryCode($country);
return $country;
} else
{
if($ipLong <$ipFrom)
{
$high = $mid - 1;
} else
{
$low = $mid + 1;
}
}
}
fclose($db_file_handle);
return "";
}
function ipdbv2_read32($db_file_handle)
{
$data = fread($db_file_handle, 4);
$output = unpack('V', $data);
$val_32_bit = $output[1];
if ($val_32_bit < 0)
$val_32_bit += 4294967296; //correct signed/unsigned issue on 32-bit platforms
return $val_32_bit;
}
Before using the function above you'll need to compile the datbase file using function below:
define( "ip2Country_binary_db_filename", "ip2country_zaigadgets.bin" );
function convert_ip2country_db_from_csv_to_binary_file_format($input_file_path = "")
{
//convert dataf from ZaiGadgets.com to binary db format
if ($input_file_path == "")
$input_file_path = "db.csv";
$output_file_path = ip2Country_binary_db_filename;
$input_file_content = file_get_contents($input_file_path); //read_text_file_content($input_file_path);
if ($input_file_content == "")
{
echo "Error: empty input db - " . $input_file_path . "<br>";
return; //nothing to do
}
unlink($output_file_path); //delete file content
$out_fileHandle = fopen($output_file_path, "cb");
if (!$out_fileHandle)
{
echo "ERROR: Cannot Create file! " . $output_file_path . "<br>";
return;
}
$records_count = 0;
$input_file_lines = explode("
", $input_file_content); //split it to lines
echo "Text lines count: " . count($input_file_lines) . "<br>";
//for ($input_line_n = 0; $input_line_n < 10; $input_line_n ++)
for ($input_line_n = 0; $input_line_n < count($input_file_lines); $input_line_n ++)
{
$sub_line = $input_file_lines[$input_line_n];
if (strlen($sub_line) > 0 && is_numeric($sub_line[0]))
{
$sub_values = explode(",", $input_file_lines[$input_line_n]);
if (count($sub_values) == 4)
{
$start_ip_addr = $sub_values[0];
$end_ip_addr = $sub_values[1];
$country_code = $sub_values[2];
if (strlen($country_code) == 2)
{
fwrite($out_fileHandle, pack("V", (float)$start_ip_addr), 4);
fwrite($out_fileHandle, pack("V", (float)$end_ip_addr), 4);
fwrite($out_fileHandle, $country_code, strlen($country_code));
$records_count ++;
} else
{
echo "Invalid line " . $input_line_n . " [wrong country len]: " . $sub_line . "<br>";
}
} else
{
echo "Invalid line " . $input_line_n . ": " . $sub_line . "<br>";
}
} else
if ($sub_line != "" && $sub_line[0] != '#' && $sub_line != "startip,endip,countrycode,countryname")
{
echo "Invalid line " . $input_line_n . ": " . $sub_line . "<br>";
}
}
fclose($out_fileHandle);
echo "Data size verification result: " . ($records_count * 10 == filesize($output_file_path) ? "OK" : "Error") . "<br>";
echo "Exported records (blocks) count: " . $records_count . "<br>";
echo "Exporting complete.<br>";
}
Now, if you like it, you can use function below to convert 2-letter country code to readable country name:
function GetCountryNameFromCountryCode($country_code, $b_very_strict = false)
{
$countries = array (
"ac" => "ASCENSION ISLAND",
"ad" => "ANDORRA",
"ae" => "UNITED ARAB EMIRATES",
"af" => "AFGHANISTAN",
"ag" => "ANTIGUA AND BARBUDA",
"ai" => "ANGUILLA",
"al" => "ALBANIA",
"am" => "ARMENIA",
"an" => "NETHERLANDS ANTILLES",
"ao" => "ANGOLA",
"aq" => "ANTARCTICA",
"ar" => "ARGENTINA",
"as" => "AMERICAN SAMOA",
"at" => "AUSTRIA",
"au" => "AUSTRALIA",
"aw" => "ARUBA",
"ax" => "ALAND",
"az" => "AZERBAIJAN",
"ba" => "BOSNIA AND HERZEGOVINA",
"bb" => "BARBADOS",
"bd" => "BANGLADESH",
"be" => "BELGIUM",
"bf" => "BURKINA FASO",
"bg" => "BULGARIA",
"bh" => "BAHRAIN",
"bi" => "BURUNDI",
"bj" => "BENIN",
"bl" => "SAINT BARTHELEMY",
"bm" => "BERMUDA",
"bn" => "BRUNEI DARUSSALAM",
"bo" => "BOLIVIA",
"bu" => "BONAIRE, SAINT EUSTATIUS AND SABA",
"br" => "BRAZIL",
"bs" => "BAHAMAS",
"bt" => "BHUTAN",
"bv" => "BOUVET ISLAND",
"bw" => "BOTSWANA",
"by" => "BELARUS",
"bz" => "BELIZE",
"ca" => "CANADA",
"cc" => "COCOS (KEELING) ISLANDS",
"cd" => "CONGO",
"cf" => "CENTRAL AFRICAN REPUBLIC",
"cg" => "REPUBLIC OF THE CONGO",
"ch" => "SWITZERLAND",
"ci" => "COTE D?VOIRE",
"ck" => "COOK ISLANDS",
"cl" => "CHILE",
"cm" => "CAMEROON",
"cn" => "CHINA",
"co" => "COLOMBIA",
"cr" => "COSTA RICA",
"cu" => "CUBA",
"cv" => "CAPE VERDE",
"cw" => "CURACAO",
"cx" => "CHRISTMAS ISLAND",
"cy" => "CYPRUS",
"cz" => "CZECH REPUBLIC",
"de" => "GERMANY",
"dj" => "DJIBOUTI",
"dk" => "DENMARK",
"dm" => "DOMINICA",
"do" => "DOMINICAN REPUBLIC",
"dz" => "ALGERIA",
"ec" => "ECUADOR",
"ee" => "ESTONIA",
"eg" => "EGYPT",
"eh" => "WESTERN SAHARA",
"er" => "ERITREA",
"eu" => "EUROPEAN UNION",
"es" => "SPAIN",
"et" => "ETHIOPIA",
"fi" => "FINLAND",
"fj" => "FIJI",
"fk" => "FALKLAND ISLANDS",
"fm" => "MICRONESIA",
"fo" => "FAROE ISLANDS",
"fr" => "FRANCE",
"ga" => "GABON",
"gb" => "UNITED KINGDOM",
"gd" => "GRENADA",
"ge" => "GEORGIA",
"gf" => "FRENCH GUIANA",
"gg" => "GUERNSEY",
"gh" => "GHANA",
"gi" => "GIBRALTAR",
"gl" => "GREENLAND",
"gm" => "THE GAMBIA",
"gn" => "GUINEA",
"gp" => "GUADELOUPE",
"gq" => "EQUATORIAL GUINEA",
"gr" => "GREECE",
"gs" => "SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS",
"gt" => "GUATEMALA",
"gu" => "GUAM",
"gw" => "GUINEA-BISSAU",
"gy" => "GUYANA",
"hk" => "HONG KONG",
"hn" => "HONDURAS",
"hr" => "CROATIA",
"ht" => "HAITI",
"hu" => "HUNGARY",
"id" => "INDONESIA",
"ie" => "IRELAND",
"il" => "ISRAEL",
"im" => "ISLE OF MAN",
"in" => "INDIA",
"io" => "BRITISH INDIAN OCEAN TERRITORY",
"iq" => "IRAQ",
"ir" => "IRAN",
"is" => "ICELAND",
"it" => "ITALY",
"je" => "JERSEY",
"jm" => "JAMAICA",
"jo" => "JORDAN",
"jp" => "JAPAN",
"ke" => "KENYA",
"kg" => "KYRGYZSTAN",
"kh" => "CAMBODIA",
"ki" => "KIRIBATI",
"km" => "COMOROS",
"kn" => "SAINT KITTS AND NEVIS",
"kp" => "KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF",
"kr" => "SOUTH KOREA",
"kw" => "KUWAIT",
"ky" => "CAYMAN ISLANDS",
"kz" => "KAZAKHSTAN",
"la" => "LAOS",
"lb" => "LEBANON",
"lc" => "SAINT LUCIA",
"li" => "LIECHTENSTEIN",
"lk" => "SRI LANKA",
"lr" => "LIBERIA",
"ls" => "LESOTHO",
"lt" => "LITHUANIA",
"lu" => "LUXEMBOURG",
"lv" => "LATVIA",
"ly" => "LIBYA",
"ma" => "MOROCCO",
"mc" => "MONACO",
"md" => "MOLDOVA",
"me" => "MONTENEGRO",
"mf" => "SAINT MARTIN (FRENCH PART)",
"mg" => "MADAGASCAR",
"mh" => "MARSHALL ISLANDS",
"mk" => "REPUBLIC OF MACEDONIA",
"ml" => "MALI",
"mm" => "MYANMAR",
"mn" => "MONGOLIA",
"mo" => "MACAO",
"mp" => "NORTHERN MARIANA ISLANDS",
"mq" => "MARTINIQUE",
"mr" => "MAURITANIA",
"ms" => "MONTSERRAT",
"mt" => "MALTA",
"mu" => "MAURITIUS",
"mv" => "MALDIVES",
"mw" => "MALAWI",
"mx" => "MEXICO",
"my" => "MALAYSIA",
"mz" => "MOZAMBIQUE",
"na" => "NAMIBIA",
"nc" => "NEW CALEDONIA",
"ne" => "NIGER",
"nf" => "NORFOLK ISLAND",
"ng" => "NIGERIA",
"ni" => "NICARAGUA",
"nl" => "NETHERLANDS",
"no" => "NORWAY",
"np" => "NEPAL",
"nr" => "NAURU",
"nu" => "NIUE",
"nz" => "NEW ZEALAND",
"om" => "OMAN",
"pa" => "PANAMA",
"pe" => "PERU",
"pf" => "FRENCH POLYNESIA",
"pg" => "PAPUA NEW GUINEA",
"ph" => "PHILIPPINES",
"pk" => "PAKISTAN",
"pl" => "POLAND",
"pm" => "SAINT PIERRE AND MIQUELON",
"pn" => "PITCAIRN ISLANDS",
"pr" => "PUERTO RICO",
"ps" => "PALESTINIAN TERRITORY, OCCUPIED",
"pt" => "PORTUGAL",
"pw" => "PALAU",
"py" => "PARAGUAY",
"qa" => "QATAR",
"re" => "REUNION",
"ro" => "ROMANIA",
"rs" => "SERBIA",
"ru" => "RUSSIA",
"rw" => "RWANDA",
"sa" => "SAUDI ARABIA",
"sb" => "SOLOMON ISLANDS",
"sc" => "SEYCHELLES",
"sd" => "SUDAN",
"se" => "SWEDEN",
"sg" => "SINGAPORE",
"sh" => "SAINT HELENA",
"si" => "SLOVENIA",
"sj" => "SVALBARD AND JAN MAYEN",
"sk" => "SLOVAKIA",
"sl" => "SIERRA LEONE",
"sm" => "SAN MARINO",
"sn" => "SENEGAL",
"so" => "SOMALIA",
"sr" => "SURINAME",
"st" => "SAO TOME AND PRINCIPE",
"su" => "RUSSIA", //ex-USSR, let's presume it's russia
"sv" => "EL SALVADOR",
"sx" => "SINT MAARTEN (DUTCH PART)",
"sy" => "SYRIA",
"sz" => "SWAZILAN",
"tc" => "TURKS AND CAICOS ISLANDS",
"td" => "CHAD",
"tf" => "FRENCH SOUTHERN TERRITORIES",
"tg" => "TOGO",
"th" => "THAILAND",
"tj" => "TAJIKISTAN",
"tk" => "TOKELAU",
"tl" => "EAST TIMOR",
"tm" => "TURKMENISTAN",
"tn" => "TUNISIA",
"to" => "TONGA",
"tp" => "EAST TIMOR",
"tr" => "TURKEY",
"tt" => "TRINIDAD AND TOBAGO",
//"tv" => "TUVALU", don't include it here - it conflicts with .tv domain!
"tw" => "TAIWAN",
"tz" => "TANZANIA",
"ua" => "UKRAINE",
"ug" => "UGANDA",
"uk" => "UNITED KINGDOM",
"um" => "UNITED STATES MINOR OUTLYING ISLAND",
"us" => "UNITED STATES",
"uy" => "URUGUAY",
"uz" => "UZBEKISTAN",
"va" => "VATICAN CITY STATE",
"vc" => "SAINT VINCENT AND THE GRENADINES",
"ve" => "VENEZUELA",
"vg" => "BRITISH VIRGIN ISLANDS",
"vi" => "U.S. VIRGIN ISLANDS",
"vn" => "VIETNAM",
"vu" => "VANUATU",
"wf" => "WALLIS AND FUTUNA",
"ws" => "SAMOA",
"ye" => "YEMEN",
"yt" => "MAYOTTE",
"yu" => "YUGOSLAVIA",
"za" => "SOUTH AFRICA",
"zm" => "ZAMBIA",
"zw" => "ZIMBABWE"
);
$country = $countries[strtolower($country_code)];
if ($country == "" && !$b_very_strict)
$country = $country_code; //return at least something
return $country;
}
And, in the end, you may find it comfortable to categorize countries be region or by wealth using functions below:
function IsUserFromEuropeanUnion($country = "")
{
if ($country == "")
$country = GetCountryFromIP();
if (strpos($country, "AUSTRIA") !== false ||
strpos($country, "BELGIUM") !== false ||
strpos($country, "BULGAR") !== false ||
strpos($country, "CYPRUS") !== false ||
strpos($country, "CZECH") !== false ||
strpos($country, "DENMARK") !== false ||
strpos($country, "ESTONIA") !== false ||
strpos($country, "FINLAND") !== false ||
strpos($country, "FRANCE") !== false ||
strpos($country, "GERMANY") !== false ||
strpos($country, "GREECE") !== false ||
strpos($country, "HUNGARY") !== false ||
strpos($country, "IRELAND") !== false ||
strpos($country, "ITALY") !== false ||
strpos($country, "LATVIA") !== false ||
strpos($country, "LEICHTEN") !== false || //is it out of EU? anyway, it's reach and almost EU
strpos($country, "MONACO") !== false || //is it out of EU? anyway, it's reach and almost EU
strpos($country, "NORWAY") !== false || //is it out of EU? anyway, it's reach and almost EU
strpos($country, "SWITZERLANS") !== false || //is it out of EU? anyway, it's reach and almost EU
strpos($country, "VATICAN") !== false || //is it out of EU? anyway, it's reach and almost EU
strpos($country, "LITHUANIA") !== false ||
strpos($country, "LUXEMBOURG") !== false ||
strpos($country, "MALTA") !== false ||
strpos($country, "NETHERL") !== false ||
strpos($country, "POLAND") !== false ||
strpos($country, "PORTUGAL") !== false ||
strpos($country, "ROMANIA") !== false ||
strpos($country, "SLOVAKIA") !== false ||
strpos($country, "SLOVENIA") !== false ||
strpos($country, "SPAIN") !== false ||
strpos($country, "SWEDEN") !== false ||
strpos($country, "IRELAND") !== false ||
strpos($country, "UNITED KINGDOM") !== false)
return true;
else
return false;
}
function IsUserFromEurope($country = "")
{
if (IsUserFromEuropeanUnion($country) == true)
return true;
if ($country == "")
$country = GetCountryFromIP();
if (strpos($country, "ALBANIA") !== false ||
strpos($country, "ANDORRA") !== false ||
//strpos($country, "ARMENIA") !== false ||
//strpos($country, "AZERBAI") !== false ||
strpos($country, "BELARUS") !== false ||
strpos($country, "BOSNIA") !== false ||
strpos($country, "HERZE") !== false ||
strpos($country, "CROATIA") !== false ||
strpos($country, "BULGARIA") !== false ||
strpos($country, "CYPRUS") !== false ||
strpos($country, "ICELAND") !== false ||
strpos($country, "IRELAND") !== false ||
strpos($country, "MACEDONIA") !== false ||
strpos($country, "MALTA") !== false ||
strpos($country, "MOLDOVA") !== false ||
strpos($country, "MONTENEGRO") !== false ||
strpos($country, "SAN MARINO") !== false ||
strpos($country, "UKRAINE") !== false ||
strpos($country, "RUSSIA") !== false ||
strpos($country, "GEORGIA") !== false ||
strpos($country, "SERBIA") !== false)
return TRUE;
else
return FALSE;
}
function IsUserFromUSorCanada($country = "")
{
if ($country == "")
$country = GetCountryFromIP();
if (strpos($country, "UNITED STATES") !== false ||
strpos($country, "NAVASSA") !== false || //USA
strpos($country, "PUERTO RICCO") !== false || //USA
strpos($country, "PUERTORICCO") !== false || //USA
strpos($country, "S VIRGIN ISLANDS") !== false || //USA
strpos($country, "S. VIRGIN ISLANDS") !== false || //USA
strpos($country, "SAMOA") !== false || //USA
strpos($country, "GUAM") !== false || //USA
strpos($country, "MARIANA ISLAND") !== false || //USA
strpos($country, "WAKE ISLAND") !== false || //USA
strpos($country, "HAWAII") !== false || //USA
strpos($country, "CANADA") !== false)
return TRUE;
else
return FALSE;
}
function IsUserFromNorthAmerica($country = "")
{
if ($country == "")
$country = GetCountryFromIP();
if (IsUserFromUSorCanada($country) == true)
return true;
if (strpos($country, "BERMUD") !== false ||
strpos($country, "GREENLAND") !== false ||
strpos($country, "MEXICO") !== false ||
strpos($country, "SAINT PIERRE") !== false ||
//Carribean countries:
strpos($country, "ANGUILLA") !== false ||
strpos($country, "ANTIGUA") !== false ||
strpos($country, "BARBUDA") !== false ||
strpos($country, "BAHAMAS") !== false ||
strpos($country, "BARBADOS") !== false ||
strpos($country, "VIRGIN ISL") !== false ||
strpos($country, "CANYMAN") !== false ||
strpos($country, "CUBA") !== false ||
strpos($country, "DOMINICA") !== false ||
strpos($country, "DOMINICAN") !== false ||
strpos($country, "GRENADA") !== false ||
strpos($country, "GUADELOUPE") !== false ||
strpos($country, "HAITI") !== false ||
strpos($country, "JAMAICA") !== false ||
strpos($country, "MARTINIQUE") !== false ||
strpos($country, "MONTSERRAT") !== false ||
strpos($country, "NAVASSA") !== false || //USA
strpos($country, "ANTILLES") !== false ||
strpos($country, "PUERTO RICCO") !== false || //USA
strpos($country, "PUERTORICCO") !== false || //USA
strpos($country, "SAINT BART") !== false ||
strpos($country, "SAINT KITTS") !== false ||
strpos($country, "SAINT LUCIA") !== false ||
strpos($country, "SAINT MARTIN") !== false ||
strpos($country, "SAINT VINCENT") !== false ||
strpos($country, "TRINIDAD") !== false ||
strpos($country, "TOBAGO") !== false ||
strpos($country, "TURKS AND CAICOS") !== false ||
strpos($country, "S VIRGIN ISLANDS") !== false || //USA
//Cental America:
strpos($country, "BELIZE") !== false ||
strpos($country, "COSTA RICA") !== false ||
strpos($country, "SALVADOR") !== false ||
strpos($country, "GUATEMALA") !== false ||
strpos($country, "HONDURAS") !== false ||
strpos($country, "NICARAGUA") !== false ||
strpos($country, "PANAMA") !== false)
return true;
else
return false;
}
function IsUserFromSouthAmerica($country = "")
{
if ($country == "")
$country = GetCountryFromIP();
if (strpos($country, "ARGENTINA") !== false ||
strpos($country, "BOLIVIA") !== false ||
strpos($country, "BRAZIL") !== false ||
strpos($country, "CHILE") !== false ||
strpos($country, "COLOMBIA") !== false ||
strpos($country, "ECUADOR") !== false ||
strpos($country, "FALKLAND") !== false ||
strpos($country, "FRENCH GUIANA") !== false ||
strpos($country, "GUYANA") !== false ||
strpos($country, "PARAGUAY") !== false ||
strpos($country, "PERU") !== false ||
strpos($country, "SOUTH GEORGIA") !== false ||
strpos($country, "SANDWICH ISL") !== false ||
strpos($country, "SURINAME") !== false ||
strpos($country, "URUGUAY") !== false ||
strpos($country, "VENEZUELLA") !== false)
return true;
else
return false;
}
function IsUserFromAsia($country = "")
{
if ($country == "")
$country = GetCountryFromIP();
if ( strpos($country, "PAKISTAN") !== false ||
strpos($country, "VIETNAM") !== false ||
strpos($country, "VIET NAM") !== false ||
strpos($country, "THAILAND") !== false ||
strpos($country, "SINGAPORE") !== false ||
strpos($country, "SAUDI") !== false ||
strpos($country, "ARABI") !== false ||
strpos($country, "BANGLADESH") !== false ||
strpos($country, "KOREA") !== false ||
strpos($country, "HONG") !== false ||
strpos($country, "MACAU") !== false ||
strpos($country, "MONGOL") !== false ||
strpos($country, "KOREA") !== false ||
strpos($country, "BRUNE") !== false ||
strpos($country, "BURMA") !== false ||
strpos($country, "CAMBODIA") !== false ||
strpos($country, "TIMOR") !== false ||
strpos($country, "LAOS") !== false ||
strpos($country, "MALAYSIA") !== false ||
strpos($country, "PHILIPPINES") !== false ||
strpos($country, "SINGAPORE") !== false ||
strpos($country, "THAILAND") !== false ||
strpos($country, "VIETNAM") !== false ||
strpos($country, "VIET NAM") !== false ||
strpos($country, "AFGHANI") !== false ||
strpos($country, "PAKISTAN") !== false ||
strpos($country, "BANGLADESH") !== false ||
strpos($country, "BHUTAN") !== false ||
strpos($country, "MALDIVES") !== false ||
strpos($country, "NEPAL") !== false ||
strpos($country, "SRI LANKA") !== false ||
strpos($country, "SRILANKA") !== false ||
strpos($country, "BAHRAIN") !== false ||
strpos($country, "IRAQ") !== false ||
strpos($country, "IRAN") !== false ||
strpos($country, "ISRAEL") !== false ||
strpos($country, "JORDAN") !== false ||
strpos($country, "KUWAIT") !== false ||
strpos($country, "LEBANON") !== false ||
strpos($country, "OMAN") !== false ||
strpos($country, "PALESTIN") !== false ||
strpos($country, "QATAR") !== false ||
strpos($country, "SAUDI") !== false ||
strpos($country, "ARABI") !== false ||
strpos($country, "SYRIA") !== false ||
strpos($country, "TURKEY") !== false ||
strpos($country, "EMIRATES") !== false ||
strpos($country, "YEMEN") !== false ||
strpos($country, "ARMENIA") !== false ||
strpos($country, "AZERBAIJAN") !== false ||
strpos($country, "KAZAKHSTAN") !== false ||
strpos($country, "KYRGYZ") !== false ||
strpos($country, "TAJIKI") !== false ||
strpos($country, "TURKMENI") !== false ||
strpos($country, "UZBEKI") !== false)
return true;
else
return false;
}
function IsUserFromVeryRichCountry($country = "")
{
//List of economy index is taken from there: https://www.cia.gov/library/publications/the-world-factbook/rankorder/2004rank.html
if ($country == "")
$country = GetCountryFromIP();
//note: top of this list is the richest
if (strpos($country, "LIECHTENSTEIN") !== false ||
strpos($country, "QATAR") !== false ||
strpos($country, "LUXEMBOURG") !== false ||
strpos($country, "BERMUDA") !== false ||
strpos($country, "NORWAY") !== false ||
strpos($country, "JERSEY") !== false ||
strpos($country, "KUWAIT") !== false ||
strpos($country, "SINGAPORE") !== false ||
strpos($country, "BRUNEI") !== false ||
strpos($country, "FAROE ISLANDS") !== false)
return true;
else
return false;
}
function IsUserFromQuiteRichCountry($country = "")
{
//List of economy index is taken from there: https://www.cia.gov/library/publications/the-world-factbook/rankorder/2004rank.html
if ($country == "")
$country = GetCountryFromIP();
//note: top of this list is the richest
if (strpos($country, "UNITED STATES") !== false ||
strpos($country, "ANDORRA") !== false ||
strpos($country, "GUERNSEY") !== false ||
strpos($country, "CAYMAN ISLANDS") !== false ||
strpos($country, "HONG KONG") !== false ||
strpos($country, "SAN MARINO") !== false ||
strpos($country, "SWITZERLAND") !== false ||
strpos($country, "IRELAND") !== false ||
strpos($country, "AUSTRALIA") !== false ||
strpos($country, "ICELAND") !== false ||
strpos($country, "NETHERLANDS") !== false ||
strpos($country, "AUSTRIA") !== false ||
strpos($country, "UNITED ARAB EMIRATES") !== false ||
strpos($country, "BAHRAIN") !== false ||
strpos($country, "BRITISH VIRGIN ISLANDS") !== false ||
strpos($country, "GIBRALTAR") !== false ||
strpos($country, "CANADA") !== false ||
strpos($country, "EQUATORIAL GUINEA") !== false ||
strpos($country, "BELGIUM") !== false ||
strpos($country, "SWEDEN") !== false ||
strpos($country, "DENMARK") !== false ||
strpos($country, "GREENLAND") !== false ||
strpos($country, "FALKLAND ISLANDS (ISLAS MALVINAS)") !== false ||
strpos($country, "ISLE OF MAN") !== false ||
strpos($country, "UNITED KINGDOM") !== false ||
strpos($country, "FINLAND") !== false ||
strpos($country, "GERMANY") !== false ||
strpos($country, "SPAIN") !== false ||
strpos($country, "MACAU") !== false ||
strpos($country, "JAPAN") !== false ||
strpos($country, "FRANCE") !== false ||
strpos($country, "EUROPEAN UNION") !== false ||
strpos($country, "TAIWAN") !== false ||
strpos($country, "GREECE") !== false ||
strpos($country, "MONACO") !== false ||
strpos($country, "ITALY") !== false)
return true;
else
return false;
}
IP2CountryWoDb PHP a script for country definition on IP, without use BD and network inquiries. (Page in Russian)
PS This post is written by me by means of the machine translator, I am sorry for possible discrepancies
I hope that by now you must have got a solution to this issue, otherwise, take a look at:
It worked for me, at least for now.
You can try the IP2IQ IP2 PHP library at https://github.com/ip2iq/ip2-lib-php which returns a country code when passed an IPv4 address. There is no SQL database needed which is a feature we wanted during development and it is faster than almost anything out there.
The only thing is that for now you will have to keep an associative array to map the returned country code to country name which can differ according to the language. You can use something like https://gist.github.com/DHS/1340150 to do that.