Determine Web Visitors Country of Origin in the Drop Down List using PHP and MySQL Database

With the emergence of online technologies such as the Internet, people and businesses have increased their reliance and use of these mediums as an avenue for commerce as it can be more convenient. During the transaction online, there are times when it is important to preset the web visitor's country of origin, ZIP code, ISP and domain name at the drop down list to prevent fraud and to ease the complexity of registration task. This article shows you how by using PHP and MYSQL Technology, it can be done.

Let us take a simple example of a user login from Canada and he needs to fill up a shopping cart. The form may be quite complex as some businesses need more information to prevent fraud. In this case, there are needs to preset certain info in the drop down list such as country of origin, ZIP code, ip and domain name of where the users login. As a result, the drop down list in this example will preset to Cananda, with the correct zip code and ip address.

In this tutorial, we use the IP2Location™ IP-Country database to lookup country of origin from the visitor's IP address. Instead of loading the full database with 50000+ records, we could simplify this tutorial with assumption only two different IP address ranges in the world. IP addresses 0.0.0.0 - 126.255.255.255 originate from United States. Meanwhile, IP addresses 127.0.0.0 - 255.255.255.255 originate from Japan. Here we are creating a database "IP2Location" with table "ip_country" that consists of two IP address range records.

Below are the steps to set up the database for both IPv4 and IPv6 data and the sample codes

Step 1: Create and connect to 'ip2location' database

CREATE DATABASE ip2location
USE ip2location

Step 2: Create 'ip_country' table

CREATE TABLE ip_country(
	ip_from DOUBLE NOT NULL,
	ip_to DOUBLE NOT NULL,
	country_code VARCHAR(2) NOT NULL,
	country_name VARCHAR(100) NOT NULL,
	PRIMARY KEY(ip_from, ip_to)
);

Step 3. Import the 'ipcountry.csv' database into table 'ip_country'

INSERT INTO ip_country VALUES (0, 2130706431,'US','UNITED STATES');
INSERT INTO ip_country VALUES (2130706432, 4294967295,'JP','JAPAN');

The full version of IP-Country database is available for subscription at $49/year from http://www.ip2location.com. If you have the full version of IP2Location™ IP-Country database, the import process is easier by using the LOAD DATA feature available in MYSQL.

mysql> LOAD DATA INFILE "/ip_country.csv" INTO TABLE ip_country FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';  

Step 4: Download country information at here. Create country table:

CREATE TABLE country(
	country_code VARCHAR(2) NOT NULL,
	country_name VARHCAR(100) NOT NULL,
	capital_name VARCHAR(100) NOT NULL,
	tld VARCHAR(2) NOT NULL,
	currency_code VARCHAR(3) NOT NULL,
	currency_name VARCHAR(50) NOT NULL,
	INDEX idx_country_code (country_code)
);

LOAD DATA INFILE "country.csv" INTO TABLE country FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'; 

Sample Code:

<?php
// Replace this MYSQL server variables with actual configuration
$mysql_server = "mysql_server.com";
$mysql_user_name = "UserName";
$mysql_user_pass = "Password";

// Retrieve visitor IP address from server variable REMOTE_ADDR
$ipaddress = getenv(REMOTE_ADDR);
// Convert IP address to IP number for querying database
$ipno = Dot2LongIP($ipaddress);
// Connect to the database server
$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
// Connect to the IP2Location database
mysql_select_db("ip2location") or die("Could not select database");

// SQL query string to match the recordset that the IP number fall between the valid range
$query = "SELECT * FROM ip_country WHERE $ipno <= ip_to LIMIT 1";
// Execute SQL query
$result = mysql_query($query) or die("IP2Location Query Failed");

// Retrieve the recordset (only one)
$row = mysql_fetch_object($result);

// Keep the country information into two different variables
$country_code = $row->country_code;

// Free recordset and close database connection
mysql_free_result($result);
mysql_close($link);

// Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to 256^4-1)
function Dot2LongIP ($IPaddr) {
  if ($IPaddr == "")
  {
    return 0;
  } else {
    $ips = split ("\.", "$IPaddr");
    return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
  }
}
?>
<select name="country">
<option value="AF"<?php if($country_code == 'AF') echo ' selected'; ?>>AFGHANISTAN</option>
<option value="AL"<?php if($country_code == 'AL') echo ' selected'; ?>>ALBANIA</option>
<option value="DZ"<?php if($country_code == 'DZ') echo ' selected'; ?>>ALGERIA</option>
<option value="AS"<?php if($country_code == 'AS') echo ' selected'; ?>>AMERICAN SAMOA</option>
<option value="AD"<?php if($country_code == 'AD') echo ' selected'; ?>>ANDORRA</option>
<option value="AO"<?php if($country_code == 'AO') echo ' selected'; ?>>ANGOLA</option>
<option value="AI"<?php if($country_code == 'AI') echo ' selected'; ?>>ANGUILLA</option>
<option value="AQ"<?php if($country_code == 'AQ') echo ' selected'; ?>>ANTARCTICA</option>
<option value="AG"<?php if($country_code == 'AG') echo ' selected'; ?>>ANTIGUA AND BARBUDA</option>
<option value="AR"<?php if($country_code == 'AR') echo ' selected'; ?>>ARGENTINA</option>
<option value="AM"<?php if($country_code == 'AM') echo ' selected'; ?>>ARMENIA</option>
<option value="AW"<?php if($country_code == 'AW') echo ' selected'; ?>>ARUBA</option>
<option value="AP"<?php if($country_code == 'AP') echo ' selected'; ?>>ASIA PACIFIC</option>
<option value="AU"<?php if($country_code == 'AU') echo ' selected'; ?>>AUSTRALIA</option>
<option value="AT"<?php if($country_code == 'AT') echo ' selected'; ?>>AUSTRIA</option>
<option value="AZ"<?php if($country_code == 'AZ') echo ' selected'; ?>>AZERBAIJAN</option>
<option value="BS"<?php if($country_code == 'BS') echo ' selected'; ?>>BAHAMAS</option>
<option value="BH"<?php if($country_code == 'BH') echo ' selected'; ?>>BAHRAIN</option>
<option value="BD"<?php if($country_code == 'BD') echo ' selected'; ?>>BANGLADESH</option>
<option value="BB"<?php if($country_code == 'BB') echo ' selected'; ?>>BARBADOS</option>
<option value="BY"<?php if($country_code == 'BY') echo ' selected'; ?>>BELARUS</option>
<option value="BE"<?php if($country_code == 'BE') echo ' selected'; ?>>BELGIUM</option>
<option value="BZ"<?php if($country_code == 'BZ') echo ' selected'; ?>>BELIZE</option>
<option value="BJ"<?php if($country_code == 'BJ') echo ' selected'; ?>>BENIN</option>
<option value="BM"<?php if($country_code == 'BM') echo ' selected'; ?>>BERMUDA</option>
<option value="BT"<?php if($country_code == 'BT') echo ' selected'; ?>>BHUTAN</option>
<option value="BO"<?php if($country_code == 'BO') echo ' selected'; ?>>BOLIVIA</option>
<option value="BA"<?php if($country_code == 'BA') echo ' selected'; ?>>BOSNIA AND HERZEGOWINA</option>
<option value="BW"<?php if($country_code == 'BW') echo ' selected'; ?>>BOTSWANA</option>
<option value="BV"<?php if($country_code == 'BV') echo ' selected'; ?>>BOUVET ISLAND</option>
<option value="BR"<?php if($country_code == 'BR') echo ' selected'; ?>>BRAZIL</option>
<option value="IO"<?php if($country_code == 'IO') echo ' selected'; ?>>BRITISH INDIAN OCEAN TERRITORY</option>
<option value="BN"<?php if($country_code == 'BN') echo ' selected'; ?>>BRUNEI DARUSSALAM</option>
<option value="BG"<?php if($country_code == 'BG') echo ' selected'; ?>>BULGARIA</option>
<option value="BF"<?php if($country_code == 'BF') echo ' selected'; ?>>BURKINA FASO</option>
<option value="BI"<?php if($country_code == 'BI') echo ' selected'; ?>>BURUNDI</option>
<option value="KH"<?php if($country_code == 'KH') echo ' selected'; ?>>CAMBODIA</option>
<option value="CM"<?php if($country_code == 'CM') echo ' selected'; ?>>CAMEROON</option>
<option value="CA"<?php if($country_code == 'CA') echo ' selected'; ?>>CANADA</option>
<option value="CV"<?php if($country_code == 'CV') echo ' selected'; ?>>CAPE VERDE</option>
<option value="KY"<?php if($country_code == 'KY') echo ' selected'; ?>>CAYMAN ISLANDS</option>
<option value="CF"<?php if($country_code == 'CF') echo ' selected'; ?>>CENTRAL AFRICAN REPUBLIC</option>
<option value="TD"<?php if($country_code == 'TD') echo ' selected'; ?>>CHAD</option>
<option value="CL"<?php if($country_code == 'CL') echo ' selected'; ?>>CHILE</option>
<option value="CN"<?php if($country_code == 'CN') echo ' selected'; ?>>CHINA</option>
<option value="CX"<?php if($country_code == 'CX') echo ' selected'; ?>>CHRISTMAS ISLAND</option>
<option value="CC"<?php if($country_code == 'CC') echo ' selected'; ?>>COCOS (KEELING) ISLANDS</option>
<option value="CO"<?php if($country_code == 'CO') echo ' selected'; ?>>COLOMBIA</option>
<option value="KM"<?php if($country_code == 'KM') echo ' selected'; ?>>COMOROS</option>
<option value="CG"<?php if($country_code == 'CG') echo ' selected'; ?>>CONGO</option>
<option value="CD"<?php if($country_code == 'CD') echo ' selected'; ?>>CONGO, THE DEMOCRATIC REPUBLIC OF THE</option>
<option value="CK"<?php if($country_code == 'CK') echo ' selected'; ?>>COOK ISLANDS</option>
<option value="CR"<?php if($country_code == 'CR') echo ' selected'; ?>>COSTA RICA</option>
<option value="CI"<?php if($country_code == 'CI') echo ' selected'; ?>>COTE D'IVOIRE</option>
<option value="HR"<?php if($country_code == 'HR') echo ' selected'; ?>>CROATIA</option>
<option value="CU"<?php if($country_code == 'CU') echo ' selected'; ?>>CUBA</option>
<option value="CY"<?php if($country_code == 'CY') echo ' selected'; ?>>CYPRUS</option>
<option value="CZ"<?php if($country_code == 'CZ') echo ' selected'; ?>>CZECH REPUBLIC</option>
<option value="CS"<?php if($country_code == 'CS') echo ' selected'; ?>>CZECHOSLOVAKIA (FORMER)</option>
<option value="DK"<?php if($country_code == 'DK') echo ' selected'; ?>>DENMARK</option>
<option value="DJ"<?php if($country_code == 'DJ') echo ' selected'; ?>>DJIBOUTI</option>
<option value="DM"<?php if($country_code == 'DM') echo ' selected'; ?>>DOMINICA</option>
<option value="DO"<?php if($country_code == 'DO') echo ' selected'; ?>>DOMINICAN REPUBLIC</option>
<option value="TP"<?php if($country_code == 'TP') echo ' selected'; ?>>EAST TIMOR</option>
<option value="EC"<?php if($country_code == 'EC') echo ' selected'; ?>>ECUADOR</option>
<option value="EG"<?php if($country_code == 'EG') echo ' selected'; ?>>EGYPT</option>
<option value="SV"<?php if($country_code == 'SV') echo ' selected'; ?>>EL SALVADOR</option>
<option value="GQ"<?php if($country_code == 'GQ') echo ' selected'; ?>>EQUATORIAL GUINEA</option>
<option value="ER"<?php if($country_code == 'ER') echo ' selected'; ?>>ERITREA</option>
<option value="EE"<?php if($country_code == 'EE') echo ' selected'; ?>>ESTONIA</option>
<option value="ET"<?php if($country_code == 'ET') echo ' selected'; ?>>ETHIOPIA</option>
<option value="EU"<?php if($country_code == 'EU') echo ' selected'; ?>>EUROPEAN UNION</option>
<option value="FK"<?php if($country_code == 'FK') echo ' selected'; ?>>FALKLAND ISLANDS (MALVINAS)</option>
<option value="FO"<?php if($country_code == 'FO') echo ' selected'; ?>>FAROE ISLANDS</option>
<option value="FJ"<?php if($country_code == 'FJ') echo ' selected'; ?>>FIJI</option>
<option value="FI"<?php if($country_code == 'FI') echo ' selected'; ?>>FINLAND</option>
<option value="FR"<?php if($country_code == 'FR') echo ' selected'; ?>>FRANCE</option>
<option value="FX"<?php if($country_code == 'FX') echo ' selected'; ?>>FRANCE, METROPOLITAN</option>
<option value="GF"<?php if($country_code == 'GF') echo ' selected'; ?>>FRENCH GUIANA</option>
<option value="PF"<?php if($country_code == 'PF') echo ' selected'; ?>>FRENCH POLYNESIA</option>
<option value="TF"<?php if($country_code == 'TF') echo ' selected'; ?>>FRENCH SOUTHERN TERRITORIES</option>
<option value="GA"<?php if($country_code == 'GA') echo ' selected'; ?>>GABON</option>
<option value="GM"<?php if($country_code == 'GM') echo ' selected'; ?>>GAMBIA</option>
<option value="GE"<?php if($country_code == 'GE') echo ' selected'; ?>>GEORGIA</option>
<option value="DE"<?php if($country_code == 'DE') echo ' selected'; ?>>GERMANY</option>
<option value="GH"<?php if($country_code == 'GH') echo ' selected'; ?>>GHANA</option>
<option value="GI"<?php if($country_code == 'GI') echo ' selected'; ?>>GIBRALTAR</option>
<option value="GB"<?php if($country_code == 'GB') echo ' selected'; ?>>GREAT BRITAIN</option>
<option value="GR"<?php if($country_code == 'GR') echo ' selected'; ?>>GREECE</option>
<option value="GL"<?php if($country_code == 'GL') echo ' selected'; ?>>GREENLAND</option>
<option value="GD"<?php if($country_code == 'GD') echo ' selected'; ?>>GRENADA</option>
<option value="GP"<?php if($country_code == 'GP') echo ' selected'; ?>>GUADELOUPE</option>
<option value="GU"<?php if($country_code == 'GU') echo ' selected'; ?>>GUAM</option>
<option value="GT"<?php if($country_code == 'GT') echo ' selected'; ?>>GUATEMALA</option>
<option value="GN"<?php if($country_code == 'GN') echo ' selected'; ?>>GUINEA</option>
<option value="GW"<?php if($country_code == 'GW') echo ' selected'; ?>>GUINEA-BISSAU</option>
<option value="GY"<?php if($country_code == 'GY') echo ' selected'; ?>>GUYANA</option>
<option value="HT"<?php if($country_code == 'HT') echo ' selected'; ?>>HAITI</option>
<option value="HM"<?php if($country_code == 'HM') echo ' selected'; ?>>HEARD ISLAND AND MCDONALD ISLANDS</option>
<option value="VA"<?php if($country_code == 'VA') echo ' selected'; ?>>HOLY SEE (VATICAN CITY STATE)</option>
<option value="HN"<?php if($country_code == 'HN') echo ' selected'; ?>>HONDURAS</option>
<option value="HK"<?php if($country_code == 'HK') echo ' selected'; ?>>HONG KONG</option>
<option value="HU"<?php if($country_code == 'HU') echo ' selected'; ?>>HUNGARY</option>
<option value="IS"<?php if($country_code == 'IS') echo ' selected'; ?>>ICELAND</option>
<option value="IN"<?php if($country_code == 'IN') echo ' selected'; ?>>INDIA</option>
<option value="ID"<?php if($country_code == 'ID') echo ' selected'; ?>>INDONESIA</option>
<option value="IR"<?php if($country_code == 'IR') echo ' selected'; ?>>IRAN, ISLAMIC REPUBLIC OF</option>
<option value="IQ"<?php if($country_code == 'IQ') echo ' selected'; ?>>IRAQ</option>
<option value="IE"<?php if($country_code == 'IE') echo ' selected'; ?>>IRELAND</option>
<option value="IL"<?php if($country_code == 'IL') echo ' selected'; ?>>ISRAEL</option>
<option value="IT"<?php if($country_code == 'IT') echo ' selected'; ?>>ITALY</option>
<option value="JM"<?php if($country_code == 'JM') echo ' selected'; ?>>JAMAICA</option>
<option value="JP"<?php if($country_code == 'JP') echo ' selected'; ?>>JAPAN</option>
<option value="JO"<?php if($country_code == 'JO') echo ' selected'; ?>>JORDAN</option>
<option value="KZ"<?php if($country_code == 'KZ') echo ' selected'; ?>>KAZAKSTAN</option>
<option value="KE"<?php if($country_code == 'KE') echo ' selected'; ?>>KENYA</option>
<option value="KI"<?php if($country_code == 'KI') echo ' selected'; ?>>KIRIBATI</option>
<option value="KP"<?php if($country_code == 'KP') echo ' selected'; ?>>KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF</option>
<option value="KR"<?php if($country_code == 'KR') echo ' selected'; ?>>KOREA, REPUBLIC OF</option>
<option value="KW"<?php if($country_code == 'KW') echo ' selected'; ?>>KUWAIT</option>
<option value="KG"<?php if($country_code == 'KG') echo ' selected'; ?>>KYRGYZSTAN</option>
<option value="LA"<?php if($country_code == 'LA') echo ' selected'; ?>>LAO PEOPLE'S DEMOCRATIC REPUBLIC</option>
<option value="LV"<?php if($country_code == 'LV') echo ' selected'; ?>>LATVIA</option>
<option value="LB"<?php if($country_code == 'LB') echo ' selected'; ?>>LEBANON</option>
<option value="LS"<?php if($country_code == 'LS') echo ' selected'; ?>>LESOTHO</option>
<option value="LR"<?php if($country_code == 'LR') echo ' selected'; ?>>LIBERIA</option>
<option value="LY"<?php if($country_code == 'LY') echo ' selected'; ?>>LIBYAN ARAB JAMAHIRIYA</option>
<option value="LI"<?php if($country_code == 'LI') echo ' selected'; ?>>LIECHTENSTEIN</option>
<option value="LT"<?php if($country_code == 'LT') echo ' selected'; ?>>LITHUANIA</option>
<option value="LU"<?php if($country_code == 'LU') echo ' selected'; ?>>LUXEMBOURG</option>
<option value="MO"<?php if($country_code == 'MO') echo ' selected'; ?>>MACAU</option>
<option value="MK"<?php if($country_code == 'MK') echo ' selected'; ?>>MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF</option>
<option value="MG"<?php if($country_code == 'MG') echo ' selected'; ?>>MADAGASCAR</option>
<option value="MW"<?php if($country_code == 'MW') echo ' selected'; ?>>MALAWI</option>
<option value="MY"<?php if($country_code == 'MY') echo ' selected'; ?>>MALAYSIA</option>
<option value="MV"<?php if($country_code == 'MV') echo ' selected'; ?>>MALDIVES</option>
<option value="ML"<?php if($country_code == 'ML') echo ' selected'; ?>>MALI</option>
<option value="MT"<?php if($country_code == 'MT') echo ' selected'; ?>>MALTA</option>
<option value="MH"<?php if($country_code == 'MH') echo ' selected'; ?>>MARSHALL ISLANDS</option>
<option value="MQ"<?php if($country_code == 'MQ') echo ' selected'; ?>>MARTINIQUE</option>
<option value="MR"<?php if($country_code == 'MR') echo ' selected'; ?>>MAURITANIA</option>
<option value="MU"<?php if($country_code == 'MU') echo ' selected'; ?>>MAURITIUS</option>
<option value="YT"<?php if($country_code == 'YT') echo ' selected'; ?>>MAYOTTE</option>
<option value="MX"<?php if($country_code == 'MX') echo ' selected'; ?>>MEXICO</option>
<option value="FM"<?php if($country_code == 'FM') echo ' selected'; ?>>MICRONESIA, FEDERATED STATES OF</option>
<option value="MD"<?php if($country_code == 'MD') echo ' selected'; ?>>MOLDOVA, REPUBLIC OF</option>
<option value="MC"<?php if($country_code == 'MC') echo ' selected'; ?>>MONACO</option>
<option value="MN"<?php if($country_code == 'MN') echo ' selected'; ?>>MONGOLIA</option>
<option value="MS"<?php if($country_code == 'MS') echo ' selected'; ?>>MONTSERRAT</option>
<option value="MA"<?php if($country_code == 'MA') echo ' selected'; ?>>MOROCCO</option>
<option value="MZ"<?php if($country_code == 'MZ') echo ' selected'; ?>>MOZAMBIQUE</option>
<option value="MM"<?php if($country_code == 'MM') echo ' selected'; ?>>MYANMAR</option>
<option value="NA"<?php if($country_code == 'NA') echo ' selected'; ?>>NAMIBIA</option>
<option value="NR"<?php if($country_code == 'NR') echo ' selected'; ?>>NAURU</option>
<option value="NP"<?php if($country_code == 'NP') echo ' selected'; ?>>NEPAL</option>
<option value="NL"<?php if($country_code == 'NL') echo ' selected'; ?>>NETHERLANDS</option>
<option value="AN"<?php if($country_code == 'AN') echo ' selected'; ?>>NETHERLANDS ANTILLES</option>
<option value="NC"<?php if($country_code == 'NC') echo ' selected'; ?>>NEW CALEDONIA</option>
<option value="NZ"<?php if($country_code == 'NZ') echo ' selected'; ?>>NEW ZEALAND</option>
<option value="NI"<?php if($country_code == 'NI') echo ' selected'; ?>>NICARAGUA</option>
<option value="NE"<?php if($country_code == 'NE') echo ' selected'; ?>>NIGER</option>
<option value="NG"<?php if($country_code == 'NG') echo ' selected'; ?>>NIGERIA</option>
<option value="NU"<?php if($country_code == 'NU') echo ' selected'; ?>>NIUE</option>
<option value="NF"<?php if($country_code == 'NF') echo ' selected'; ?>>NORFOLK ISLAND</option>
<option value="MP"<?php if($country_code == 'MP') echo ' selected'; ?>>NORTHERN MARIANA ISLANDS</option>
<option value="NO"<?php if($country_code == 'NO') echo ' selected'; ?>>NORWAY</option>
<option value="OM"<?php if($country_code == 'OM') echo ' selected'; ?>>OMAN</option>
<option value="PK"<?php if($country_code == 'PK') echo ' selected'; ?>>PAKISTAN</option>
<option value="PW"<?php if($country_code == 'PW') echo ' selected'; ?>>PALAU</option>
<option value="PS"<?php if($country_code == 'PS') echo ' selected'; ?>>PALESTINIAN TERRITORY, OCCUPIED</option>
<option value="PA"<?php if($country_code == 'PA') echo ' selected'; ?>>PANAMA</option>
<option value="PG"<?php if($country_code == 'PG') echo ' selected'; ?>>PAPUA NEW GUINEA</option>
<option value="PY"<?php if($country_code == 'PY') echo ' selected'; ?>>PARAGUAY</option>
<option value="PE"<?php if($country_code == 'PE') echo ' selected'; ?>>PERU</option>
<option value="PH"<?php if($country_code == 'PH') echo ' selected'; ?>>PHILIPPINES</option>
<option value="PN"<?php if($country_code == 'PN') echo ' selected'; ?>>PITCAIRN</option>
<option value="PL"<?php if($country_code == 'PL') echo ' selected'; ?>>POLAND</option>
<option value="PT"<?php if($country_code == 'PT') echo ' selected'; ?>>PORTUGAL</option>
<option value="PR"<?php if($country_code == 'PR') echo ' selected'; ?>>PUERTO RICO</option>
<option value="QA"<?php if($country_code == 'QA') echo ' selected'; ?>>QATAR</option>
<option value="RE"<?php if($country_code == 'RE') echo ' selected'; ?>>REUNION</option>
<option value="RO"<?php if($country_code == 'RO') echo ' selected'; ?>>ROMANIA</option>
<option value="SU"<?php if($country_code == 'SU') echo ' selected'; ?>>RUSSIAN FEDERATION</option>
<option value="RW"<?php if($country_code == 'RW') echo ' selected'; ?>>RWANDA</option>
<option value="SH"<?php if($country_code == 'SH') echo ' selected'; ?>>SAINT HELENA</option>
<option value="KN"<?php if($country_code == 'KN') echo ' selected'; ?>>SAINT KITTS AND NEVIS</option>
<option value="LC"<?php if($country_code == 'LC') echo ' selected'; ?>>SAINT LUCIA</option>
<option value="PM"<?php if($country_code == 'PM') echo ' selected'; ?>>SAINT PIERRE AND MIQUELON</option>
<option value="VC"<?php if($country_code == 'VC') echo ' selected'; ?>>SAINT VINCENT AND THE GRENADINES</option>
<option value="WS"<?php if($country_code == 'WS') echo ' selected'; ?>>SAMOA</option>
<option value="SM"<?php if($country_code == 'SM') echo ' selected'; ?>>SAN MARINO</option>
<option value="ST"<?php if($country_code == 'ST') echo ' selected'; ?>>SAO TOME AND PRINCIPE</option>
<option value="SA"<?php if($country_code == 'SA') echo ' selected'; ?>>SAUDI ARABIA</option>
<option value="SN"<?php if($country_code == 'SN') echo ' selected'; ?>>SENEGAL</option>
<option value="YU"<?php if($country_code == 'YU') echo ' selected'; ?>>SERBIA AND MONTENEGRO</option>
<option value="SC"<?php if($country_code == 'SC') echo ' selected'; ?>>SEYCHELLES</option>
<option value="SL"<?php if($country_code == 'SL') echo ' selected'; ?>>SIERRA LEONE</option>
<option value="SG"<?php if($country_code == 'SG') echo ' selected'; ?>>SINGAPORE</option>
<option value="SK"<?php if($country_code == 'SK') echo ' selected'; ?>>SLOVAKIA</option>
<option value="SI"<?php if($country_code == 'SI') echo ' selected'; ?>>SLOVENIA</option>
<option value="SB"<?php if($country_code == 'SB') echo ' selected'; ?>>SOLOMON ISLANDS</option>
<option value="SO"<?php if($country_code == 'SO') echo ' selected'; ?>>SOMALIA</option>
<option value="ZA"<?php if($country_code == 'ZA') echo ' selected'; ?>>SOUTH AFRICA</option>
<option value="GS"<?php if($country_code == 'GS') echo ' selected'; ?>>SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS</option>
<option value="ES"<?php if($country_code == 'ES') echo ' selected'; ?>>SPAIN</option>
<option value="LK"<?php if($country_code == 'LK') echo ' selected'; ?>>SRI LANKA</option>
<option value="SD"<?php if($country_code == 'SD') echo ' selected'; ?>>SUDAN</option>
<option value="SR"<?php if($country_code == 'SR') echo ' selected'; ?>>SURINAME</option>
<option value="SJ"<?php if($country_code == 'SJ') echo ' selected'; ?>>SVALBARD AND JAN MAYEN</option>
<option value="SZ"<?php if($country_code == 'SZ') echo ' selected'; ?>>SWAZILAND</option>
<option value="SE"<?php if($country_code == 'SE') echo ' selected'; ?>>SWEDEN</option>
<option value="CH"<?php if($country_code == 'CH') echo ' selected'; ?>>SWITZERLAND</option>
<option value="SY"<?php if($country_code == 'SY') echo ' selected'; ?>>SYRIAN ARAB REPUBLIC</option>
<option value="TW"<?php if($country_code == 'TW') echo ' selected'; ?>>TAIWAN, PROVINCE OF CHINA</option>
<option value="TJ"<?php if($country_code == 'TJ') echo ' selected'; ?>>TAJIKISTAN</option>
<option value="TZ"<?php if($country_code == 'TZ') echo ' selected'; ?>>TANZANIA, UNITED REPUBLIC OF</option>
<option value="TH"<?php if($country_code == 'TH') echo ' selected'; ?>>THAILAND</option>
<option value="TG"<?php if($country_code == 'TG') echo ' selected'; ?>>TOGO</option>
<option value="TK"<?php if($country_code == 'TK') echo ' selected'; ?>>TOKELAU</option>
<option value="TO"<?php if($country_code == 'TO') echo ' selected'; ?>>TONGA</option>
<option value="TT"<?php if($country_code == 'TT') echo ' selected'; ?>>TRINIDAD AND TOBAGO</option>
<option value="TN"<?php if($country_code == 'TN') echo ' selected'; ?>>TUNISIA</option>
<option value="TR"<?php if($country_code == 'TR') echo ' selected'; ?>>TURKEY</option>
<option value="TM"<?php if($country_code == 'TM') echo ' selected'; ?>>TURKMENISTAN</option>
<option value="TC"<?php if($country_code == 'TC') echo ' selected'; ?>>TURKS AND CAICOS ISLANDS</option>
<option value="TV"<?php if($country_code == 'TV') echo ' selected'; ?>>TUVALU</option>
<option value="UG"<?php if($country_code == 'UG') echo ' selected'; ?>>UGANDA</option>
<option value="UA"<?php if($country_code == 'UA') echo ' selected'; ?>>UKRAINE</option>
<option value="AE"<?php if($country_code == 'AE') echo ' selected'; ?>>UNITED ARAB EMIRATES</option>
<option value="UK"<?php if($country_code == 'UK') echo ' selected'; ?>>UNITED KINGDOM</option>
<option value="US"<?php if($country_code == 'US') echo ' selected'; ?>>UNITED STATES</option>
<option value="UM"<?php if($country_code == 'UM') echo ' selected'; ?>>UNITED STATES MINOR OUTLYING ISLANDS</option>
<option value="UY"<?php if($country_code == 'UY') echo ' selected'; ?>>URUGUAY</option>
<option value="UZ"<?php if($country_code == 'UZ') echo ' selected'; ?>>UZBEKISTAN</option>
<option value="VU"<?php if($country_code == 'VU') echo ' selected'; ?>>VANUATU</option>
<option value="VE"<?php if($country_code == 'VE') echo ' selected'; ?>>VENEZUELA</option>
<option value="VN"<?php if($country_code == 'VN') echo ' selected'; ?>>VIET NAM</option>
<option value="VG"<?php if($country_code == 'VG') echo ' selected'; ?>>VIRGIN ISLANDS, BRITISH</option>
<option value="VI"<?php if($country_code == 'VI') echo ' selected'; ?>>VIRGIN ISLANDS, U.S.</option>
<option value="WF"<?php if($country_code == 'WF') echo ' selected'; ?>>WALLIS AND FUTUNA</option>
<option value="EH"<?php if($country_code == 'EH') echo ' selected'; ?>>WESTERN SAHARA</option>
<option value="YE"<?php if($country_code == 'YE') echo ' selected'; ?>>YEMEN</option>
<option value="ZM"<?php if($country_code == 'ZM') echo ' selected'; ?>>ZAMBIA</option>
<option value="ZW"<?php if($country_code == 'ZW') echo ' selected'; ?>>ZIMBABWE</option>
</select>

 

Step 1: Create and connect to 'ip2location' database

CREATE DATABASE ip2location;
USE ip2location;

Step 2: Create 'ip_country' table

CREATE TABLE `ip_country`(
	`ip_from` DECIMAL(39,0) UNSIGNED,
	`ip_to` DECIMAL(39,0) UNSIGNED,
	`country_code` CHAR(2),
	`country_name` VARCHAR(64),
	INDEX `idx_ip_from` (`ip_from`),
	INDEX `idx_ip_to` (`ip_to`),
	INDEX `idx_ip_from_to` (`ip_from`, `ip_to`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

Step 3. Import the 'ipcountry.csv' database into table 'ip_country'

INSERT INTO ip_country VALUES (0, 170141183460469231731687303715884105727,'US','UNITED STATES');
INSERT INTO ip_country VALUES (170141183460469231731687303715884105728, 340282366920938463463374607431768211455,'JP','JAPAN'); 

The full version of IP-Country database is available for subscription at $49/year from http://www.ip2location.com. If you have the full version of IP2Location™ IP-Country database, the import process is easier by using the LOAD DATA feature available in MYSQL.

mysql> LOAD DATA INFILE "/ip_country.csv" INTO TABLE ip_country FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';  

Step 4: Download country information at here. Create country table:

CREATE TABLE country(
	country_code CHAR(2) NOT NULL,
	country_name VARCHAR(64) NOT NULL,
	capital_name VARCHAR(100) NOT NULL,
	tld VARCHAR(2) NOT NULL,
	currency_code VARCHAR(3) NOT NULL,
	currency_name VARCHAR(50) NOT NULL,
	INDEX idx_country_code (country_code)
);

LOAD DATA INFILE "country.csv" INTO TABLE country FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'; 

Sample Code:

<?php
// Replace this MYSQL server variables with actual configuration
$mysql_server = "mysql_server.com";
$mysql_user_name = "UserName";
$mysql_user_pass = "Password";

// Retrieve visitor IP address from server variable REMOTE_ADDR
$ipaddress = getenv(REMOTE_ADDR);
// Convert IP address to IP number for querying database
$ipno = Dot2LongIPv6($ipaddress);
// Connect to the database server
$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
// Connect to the IP2Location database
mysql_select_db("ip2location") or die("Could not select database");

// SQL query string to match the recordset that the IP number fall between the valid range
$query = "SELECT * FROM ip_country WHERE $ipno <= ip_to LIMIT 1";
// Execute SQL query
$result = mysql_query($query) or die("IP2Location Query Failed");

// Retrieve the recordset (only one)
$row = mysql_fetch_object($result);

// Keep the country information into two different variables
$country_code = $row->country_code;

// Free recordset and close database connection
mysql_free_result($result);
mysql_close($link);

// Function to convert IP address to IP number (IPv6)
function Dot2LongIPv6 ($IPaddr) {
    $int = inet_pton($IPaddr);
    $bits = 15;
    $ipv6long = 0;
    while($bits >= 0){
        $bin = sprintf("%08b", (ord($int[$bits])));
        if($ipv6long){
            $ipv6long = $bin . $ipv6long;
        }
        else{
            $ipv6long = $bin;
        }
        $bits--;
    }
    $ipv6long = gmp_strval(gmp_init($ipv6long, 2), 10);
    return $ipv6long;
}
?>
<select name="country">
<option value="AF"<?php if($country_code == 'AF') echo ' selected'; ?>>AFGHANISTAN</option>
<option value="AL"<?php if($country_code == 'AL') echo ' selected'; ?>>ALBANIA</option>
<option value="DZ"<?php if($country_code == 'DZ') echo ' selected'; ?>>ALGERIA</option>
<option value="AS"<?php if($country_code == 'AS') echo ' selected'; ?>>AMERICAN SAMOA</option>
<option value="AD"<?php if($country_code == 'AD') echo ' selected'; ?>>ANDORRA</option>
<option value="AO"<?php if($country_code == 'AO') echo ' selected'; ?>>ANGOLA</option>
<option value="AI"<?php if($country_code == 'AI') echo ' selected'; ?>>ANGUILLA</option>
<option value="AQ"<?php if($country_code == 'AQ') echo ' selected'; ?>>ANTARCTICA</option>
<option value="AG"<?php if($country_code == 'AG') echo ' selected'; ?>>ANTIGUA AND BARBUDA</option>
<option value="AR"<?php if($country_code == 'AR') echo ' selected'; ?>>ARGENTINA</option>
<option value="AM"<?php if($country_code == 'AM') echo ' selected'; ?>>ARMENIA</option>
<option value="AW"<?php if($country_code == 'AW') echo ' selected'; ?>>ARUBA</option>
<option value="AP"<?php if($country_code == 'AP') echo ' selected'; ?>>ASIA PACIFIC</option>
<option value="AU"<?php if($country_code == 'AU') echo ' selected'; ?>>AUSTRALIA</option>
<option value="AT"<?php if($country_code == 'AT') echo ' selected'; ?>>AUSTRIA</option>
<option value="AZ"<?php if($country_code == 'AZ') echo ' selected'; ?>>AZERBAIJAN</option>
<option value="BS"<?php if($country_code == 'BS') echo ' selected'; ?>>BAHAMAS</option>
<option value="BH"<?php if($country_code == 'BH') echo ' selected'; ?>>BAHRAIN</option>
<option value="BD"<?php if($country_code == 'BD') echo ' selected'; ?>>BANGLADESH</option>
<option value="BB"<?php if($country_code == 'BB') echo ' selected'; ?>>BARBADOS</option>
<option value="BY"<?php if($country_code == 'BY') echo ' selected'; ?>>BELARUS</option>
<option value="BE"<?php if($country_code == 'BE') echo ' selected'; ?>>BELGIUM</option>
<option value="BZ"<?php if($country_code == 'BZ') echo ' selected'; ?>>BELIZE</option>
<option value="BJ"<?php if($country_code == 'BJ') echo ' selected'; ?>>BENIN</option>
<option value="BM"<?php if($country_code == 'BM') echo ' selected'; ?>>BERMUDA</option>
<option value="BT"<?php if($country_code == 'BT') echo ' selected'; ?>>BHUTAN</option>
<option value="BO"<?php if($country_code == 'BO') echo ' selected'; ?>>BOLIVIA</option>
<option value="BA"<?php if($country_code == 'BA') echo ' selected'; ?>>BOSNIA AND HERZEGOWINA</option>
<option value="BW"<?php if($country_code == 'BW') echo ' selected'; ?>>BOTSWANA</option>
<option value="BV"<?php if($country_code == 'BV') echo ' selected'; ?>>BOUVET ISLAND</option>
<option value="BR"<?php if($country_code == 'BR') echo ' selected'; ?>>BRAZIL</option>
<option value="IO"<?php if($country_code == 'IO') echo ' selected'; ?>>BRITISH INDIAN OCEAN TERRITORY</option>
<option value="BN"<?php if($country_code == 'BN') echo ' selected'; ?>>BRUNEI DARUSSALAM</option>
<option value="BG"<?php if($country_code == 'BG') echo ' selected'; ?>>BULGARIA</option>
<option value="BF"<?php if($country_code == 'BF') echo ' selected'; ?>>BURKINA FASO</option>
<option value="BI"<?php if($country_code == 'BI') echo ' selected'; ?>>BURUNDI</option>
<option value="KH"<?php if($country_code == 'KH') echo ' selected'; ?>>CAMBODIA</option>
<option value="CM"<?php if($country_code == 'CM') echo ' selected'; ?>>CAMEROON</option>
<option value="CA"<?php if($country_code == 'CA') echo ' selected'; ?>>CANADA</option>
<option value="CV"<?php if($country_code == 'CV') echo ' selected'; ?>>CAPE VERDE</option>
<option value="KY"<?php if($country_code == 'KY') echo ' selected'; ?>>CAYMAN ISLANDS</option>
<option value="CF"<?php if($country_code == 'CF') echo ' selected'; ?>>CENTRAL AFRICAN REPUBLIC</option>
<option value="TD"<?php if($country_code == 'TD') echo ' selected'; ?>>CHAD</option>
<option value="CL"<?php if($country_code == 'CL') echo ' selected'; ?>>CHILE</option>
<option value="CN"<?php if($country_code == 'CN') echo ' selected'; ?>>CHINA</option>
<option value="CX"<?php if($country_code == 'CX') echo ' selected'; ?>>CHRISTMAS ISLAND</option>
<option value="CC"<?php if($country_code == 'CC') echo ' selected'; ?>>COCOS (KEELING) ISLANDS</option>
<option value="CO"<?php if($country_code == 'CO') echo ' selected'; ?>>COLOMBIA</option>
<option value="KM"<?php if($country_code == 'KM') echo ' selected'; ?>>COMOROS</option>
<option value="CG"<?php if($country_code == 'CG') echo ' selected'; ?>>CONGO</option>
<option value="CD"<?php if($country_code == 'CD') echo ' selected'; ?>>CONGO, THE DEMOCRATIC REPUBLIC OF THE</option>
<option value="CK"<?php if($country_code == 'CK') echo ' selected'; ?>>COOK ISLANDS</option>
<option value="CR"<?php if($country_code == 'CR') echo ' selected'; ?>>COSTA RICA</option>
<option value="CI"<?php if($country_code == 'CI') echo ' selected'; ?>>COTE D'IVOIRE</option>
<option value="HR"<?php if($country_code == 'HR') echo ' selected'; ?>>CROATIA</option>
<option value="CU"<?php if($country_code == 'CU') echo ' selected'; ?>>CUBA</option>
<option value="CY"<?php if($country_code == 'CY') echo ' selected'; ?>>CYPRUS</option>
<option value="CZ"<?php if($country_code == 'CZ') echo ' selected'; ?>>CZECH REPUBLIC</option>
<option value="CS"<?php if($country_code == 'CS') echo ' selected'; ?>>CZECHOSLOVAKIA (FORMER)</option>
<option value="DK"<?php if($country_code == 'DK') echo ' selected'; ?>>DENMARK</option>
<option value="DJ"<?php if($country_code == 'DJ') echo ' selected'; ?>>DJIBOUTI</option>
<option value="DM"<?php if($country_code == 'DM') echo ' selected'; ?>>DOMINICA</option>
<option value="DO"<?php if($country_code == 'DO') echo ' selected'; ?>>DOMINICAN REPUBLIC</option>
<option value="TP"<?php if($country_code == 'TP') echo ' selected'; ?>>EAST TIMOR</option>
<option value="EC"<?php if($country_code == 'EC') echo ' selected'; ?>>ECUADOR</option>
<option value="EG"<?php if($country_code == 'EG') echo ' selected'; ?>>EGYPT</option>
<option value="SV"<?php if($country_code == 'SV') echo ' selected'; ?>>EL SALVADOR</option>
<option value="GQ"<?php if($country_code == 'GQ') echo ' selected'; ?>>EQUATORIAL GUINEA</option>
<option value="ER"<?php if($country_code == 'ER') echo ' selected'; ?>>ERITREA</option>
<option value="EE"<?php if($country_code == 'EE') echo ' selected'; ?>>ESTONIA</option>
<option value="ET"<?php if($country_code == 'ET') echo ' selected'; ?>>ETHIOPIA</option>
<option value="EU"<?php if($country_code == 'EU') echo ' selected'; ?>>EUROPEAN UNION</option>
<option value="FK"<?php if($country_code == 'FK') echo ' selected'; ?>>FALKLAND ISLANDS (MALVINAS)</option>
<option value="FO"<?php if($country_code == 'FO') echo ' selected'; ?>>FAROE ISLANDS</option>
<option value="FJ"<?php if($country_code == 'FJ') echo ' selected'; ?>>FIJI</option>
<option value="FI"<?php if($country_code == 'FI') echo ' selected'; ?>>FINLAND</option>
<option value="FR"<?php if($country_code == 'FR') echo ' selected'; ?>>FRANCE</option>
<option value="FX"<?php if($country_code == 'FX') echo ' selected'; ?>>FRANCE, METROPOLITAN</option>
<option value="GF"<?php if($country_code == 'GF') echo ' selected'; ?>>FRENCH GUIANA</option>
<option value="PF"<?php if($country_code == 'PF') echo ' selected'; ?>>FRENCH POLYNESIA</option>
<option value="TF"<?php if($country_code == 'TF') echo ' selected'; ?>>FRENCH SOUTHERN TERRITORIES</option>
<option value="GA"<?php if($country_code == 'GA') echo ' selected'; ?>>GABON</option>
<option value="GM"<?php if($country_code == 'GM') echo ' selected'; ?>>GAMBIA</option>
<option value="GE"<?php if($country_code == 'GE') echo ' selected'; ?>>GEORGIA</option>
<option value="DE"<?php if($country_code == 'DE') echo ' selected'; ?>>GERMANY</option>
<option value="GH"<?php if($country_code == 'GH') echo ' selected'; ?>>GHANA</option>
<option value="GI"<?php if($country_code == 'GI') echo ' selected'; ?>>GIBRALTAR</option>
<option value="GB"<?php if($country_code == 'GB') echo ' selected'; ?>>GREAT BRITAIN</option>
<option value="GR"<?php if($country_code == 'GR') echo ' selected'; ?>>GREECE</option>
<option value="GL"<?php if($country_code == 'GL') echo ' selected'; ?>>GREENLAND</option>
<option value="GD"<?php if($country_code == 'GD') echo ' selected'; ?>>GRENADA</option>
<option value="GP"<?php if($country_code == 'GP') echo ' selected'; ?>>GUADELOUPE</option>
<option value="GU"<?php if($country_code == 'GU') echo ' selected'; ?>>GUAM</option>
<option value="GT"<?php if($country_code == 'GT') echo ' selected'; ?>>GUATEMALA</option>
<option value="GN"<?php if($country_code == 'GN') echo ' selected'; ?>>GUINEA</option>
<option value="GW"<?php if($country_code == 'GW') echo ' selected'; ?>>GUINEA-BISSAU</option>
<option value="GY"<?php if($country_code == 'GY') echo ' selected'; ?>>GUYANA</option>
<option value="HT"<?php if($country_code == 'HT') echo ' selected'; ?>>HAITI</option>
<option value="HM"<?php if($country_code == 'HM') echo ' selected'; ?>>HEARD ISLAND AND MCDONALD ISLANDS</option>
<option value="VA"<?php if($country_code == 'VA') echo ' selected'; ?>>HOLY SEE (VATICAN CITY STATE)</option>
<option value="HN"<?php if($country_code == 'HN') echo ' selected'; ?>>HONDURAS</option>
<option value="HK"<?php if($country_code == 'HK') echo ' selected'; ?>>HONG KONG</option>
<option value="HU"<?php if($country_code == 'HU') echo ' selected'; ?>>HUNGARY</option>
<option value="IS"<?php if($country_code == 'IS') echo ' selected'; ?>>ICELAND</option>
<option value="IN"<?php if($country_code == 'IN') echo ' selected'; ?>>INDIA</option>
<option value="ID"<?php if($country_code == 'ID') echo ' selected'; ?>>INDONESIA</option>
<option value="IR"<?php if($country_code == 'IR') echo ' selected'; ?>>IRAN, ISLAMIC REPUBLIC OF</option>
<option value="IQ"<?php if($country_code == 'IQ') echo ' selected'; ?>>IRAQ</option>
<option value="IE"<?php if($country_code == 'IE') echo ' selected'; ?>>IRELAND</option>
<option value="IL"<?php if($country_code == 'IL') echo ' selected'; ?>>ISRAEL</option>
<option value="IT"<?php if($country_code == 'IT') echo ' selected'; ?>>ITALY</option>
<option value="JM"<?php if($country_code == 'JM') echo ' selected'; ?>>JAMAICA</option>
<option value="JP"<?php if($country_code == 'JP') echo ' selected'; ?>>JAPAN</option>
<option value="JO"<?php if($country_code == 'JO') echo ' selected'; ?>>JORDAN</option>
<option value="KZ"<?php if($country_code == 'KZ') echo ' selected'; ?>>KAZAKSTAN</option>
<option value="KE"<?php if($country_code == 'KE') echo ' selected'; ?>>KENYA</option>
<option value="KI"<?php if($country_code == 'KI') echo ' selected'; ?>>KIRIBATI</option>
<option value="KP"<?php if($country_code == 'KP') echo ' selected'; ?>>KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF</option>
<option value="KR"<?php if($country_code == 'KR') echo ' selected'; ?>>KOREA, REPUBLIC OF</option>
<option value="KW"<?php if($country_code == 'KW') echo ' selected'; ?>>KUWAIT</option>
<option value="KG"<?php if($country_code == 'KG') echo ' selected'; ?>>KYRGYZSTAN</option>
<option value="LA"<?php if($country_code == 'LA') echo ' selected'; ?>>LAO PEOPLE'S DEMOCRATIC REPUBLIC</option>
<option value="LV"<?php if($country_code == 'LV') echo ' selected'; ?>>LATVIA</option>
<option value="LB"<?php if($country_code == 'LB') echo ' selected'; ?>>LEBANON</option>
<option value="LS"<?php if($country_code == 'LS') echo ' selected'; ?>>LESOTHO</option>
<option value="LR"<?php if($country_code == 'LR') echo ' selected'; ?>>LIBERIA</option>
<option value="LY"<?php if($country_code == 'LY') echo ' selected'; ?>>LIBYAN ARAB JAMAHIRIYA</option>
<option value="LI"<?php if($country_code == 'LI') echo ' selected'; ?>>LIECHTENSTEIN</option>
<option value="LT"<?php if($country_code == 'LT') echo ' selected'; ?>>LITHUANIA</option>
<option value="LU"<?php if($country_code == 'LU') echo ' selected'; ?>>LUXEMBOURG</option>
<option value="MO"<?php if($country_code == 'MO') echo ' selected'; ?>>MACAU</option>
<option value="MK"<?php if($country_code == 'MK') echo ' selected'; ?>>MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF</option>
<option value="MG"<?php if($country_code == 'MG') echo ' selected'; ?>>MADAGASCAR</option>
<option value="MW"<?php if($country_code == 'MW') echo ' selected'; ?>>MALAWI</option>
<option value="MY"<?php if($country_code == 'MY') echo ' selected'; ?>>MALAYSIA</option>
<option value="MV"<?php if($country_code == 'MV') echo ' selected'; ?>>MALDIVES</option>
<option value="ML"<?php if($country_code == 'ML') echo ' selected'; ?>>MALI</option>
<option value="MT"<?php if($country_code == 'MT') echo ' selected'; ?>>MALTA</option>
<option value="MH"<?php if($country_code == 'MH') echo ' selected'; ?>>MARSHALL ISLANDS</option>
<option value="MQ"<?php if($country_code == 'MQ') echo ' selected'; ?>>MARTINIQUE</option>
<option value="MR"<?php if($country_code == 'MR') echo ' selected'; ?>>MAURITANIA</option>
<option value="MU"<?php if($country_code == 'MU') echo ' selected'; ?>>MAURITIUS</option>
<option value="YT"<?php if($country_code == 'YT') echo ' selected'; ?>>MAYOTTE</option>
<option value="MX"<?php if($country_code == 'MX') echo ' selected'; ?>>MEXICO</option>
<option value="FM"<?php if($country_code == 'FM') echo ' selected'; ?>>MICRONESIA, FEDERATED STATES OF</option>
<option value="MD"<?php if($country_code == 'MD') echo ' selected'; ?>>MOLDOVA, REPUBLIC OF</option>
<option value="MC"<?php if($country_code == 'MC') echo ' selected'; ?>>MONACO</option>
<option value="MN"<?php if($country_code == 'MN') echo ' selected'; ?>>MONGOLIA</option>
<option value="MS"<?php if($country_code == 'MS') echo ' selected'; ?>>MONTSERRAT</option>
<option value="MA"<?php if($country_code == 'MA') echo ' selected'; ?>>MOROCCO</option>
<option value="MZ"<?php if($country_code == 'MZ') echo ' selected'; ?>>MOZAMBIQUE</option>
<option value="MM"<?php if($country_code == 'MM') echo ' selected'; ?>>MYANMAR</option>
<option value="NA"<?php if($country_code == 'NA') echo ' selected'; ?>>NAMIBIA</option>
<option value="NR"<?php if($country_code == 'NR') echo ' selected'; ?>>NAURU</option>
<option value="NP"<?php if($country_code == 'NP') echo ' selected'; ?>>NEPAL</option>
<option value="NL"<?php if($country_code == 'NL') echo ' selected'; ?>>NETHERLANDS</option>
<option value="AN"<?php if($country_code == 'AN') echo ' selected'; ?>>NETHERLANDS ANTILLES</option>
<option value="NC"<?php if($country_code == 'NC') echo ' selected'; ?>>NEW CALEDONIA</option>
<option value="NZ"<?php if($country_code == 'NZ') echo ' selected'; ?>>NEW ZEALAND</option>
<option value="NI"<?php if($country_code == 'NI') echo ' selected'; ?>>NICARAGUA</option>
<option value="NE"<?php if($country_code == 'NE') echo ' selected'; ?>>NIGER</option>
<option value="NG"<?php if($country_code == 'NG') echo ' selected'; ?>>NIGERIA</option>
<option value="NU"<?php if($country_code == 'NU') echo ' selected'; ?>>NIUE</option>
<option value="NF"<?php if($country_code == 'NF') echo ' selected'; ?>>NORFOLK ISLAND</option>
<option value="MP"<?php if($country_code == 'MP') echo ' selected'; ?>>NORTHERN MARIANA ISLANDS</option>
<option value="NO"<?php if($country_code == 'NO') echo ' selected'; ?>>NORWAY</option>
<option value="OM"<?php if($country_code == 'OM') echo ' selected'; ?>>OMAN</option>
<option value="PK"<?php if($country_code == 'PK') echo ' selected'; ?>>PAKISTAN</option>
<option value="PW"<?php if($country_code == 'PW') echo ' selected'; ?>>PALAU</option>
<option value="PS"<?php if($country_code == 'PS') echo ' selected'; ?>>PALESTINIAN TERRITORY, OCCUPIED</option>
<option value="PA"<?php if($country_code == 'PA') echo ' selected'; ?>>PANAMA</option>
<option value="PG"<?php if($country_code == 'PG') echo ' selected'; ?>>PAPUA NEW GUINEA</option>
<option value="PY"<?php if($country_code == 'PY') echo ' selected'; ?>>PARAGUAY</option>
<option value="PE"<?php if($country_code == 'PE') echo ' selected'; ?>>PERU</option>
<option value="PH"<?php if($country_code == 'PH') echo ' selected'; ?>>PHILIPPINES</option>
<option value="PN"<?php if($country_code == 'PN') echo ' selected'; ?>>PITCAIRN</option>
<option value="PL"<?php if($country_code == 'PL') echo ' selected'; ?>>POLAND</option>
<option value="PT"<?php if($country_code == 'PT') echo ' selected'; ?>>PORTUGAL</option>
<option value="PR"<?php if($country_code == 'PR') echo ' selected'; ?>>PUERTO RICO</option>
<option value="QA"<?php if($country_code == 'QA') echo ' selected'; ?>>QATAR</option>
<option value="RE"<?php if($country_code == 'RE') echo ' selected'; ?>>REUNION</option>
<option value="RO"<?php if($country_code == 'RO') echo ' selected'; ?>>ROMANIA</option>
<option value="SU"<?php if($country_code == 'SU') echo ' selected'; ?>>RUSSIAN FEDERATION</option>
<option value="RW"<?php if($country_code == 'RW') echo ' selected'; ?>>RWANDA</option>
<option value="SH"<?php if($country_code == 'SH') echo ' selected'; ?>>SAINT HELENA</option>
<option value="KN"<?php if($country_code == 'KN') echo ' selected'; ?>>SAINT KITTS AND NEVIS</option>
<option value="LC"<?php if($country_code == 'LC') echo ' selected'; ?>>SAINT LUCIA</option>
<option value="PM"<?php if($country_code == 'PM') echo ' selected'; ?>>SAINT PIERRE AND MIQUELON</option>
<option value="VC"<?php if($country_code == 'VC') echo ' selected'; ?>>SAINT VINCENT AND THE GRENADINES</option>
<option value="WS"<?php if($country_code == 'WS') echo ' selected'; ?>>SAMOA</option>
<option value="SM"<?php if($country_code == 'SM') echo ' selected'; ?>>SAN MARINO</option>
<option value="ST"<?php if($country_code == 'ST') echo ' selected'; ?>>SAO TOME AND PRINCIPE</option>
<option value="SA"<?php if($country_code == 'SA') echo ' selected'; ?>>SAUDI ARABIA</option>
<option value="SN"<?php if($country_code == 'SN') echo ' selected'; ?>>SENEGAL</option>
<option value="YU"<?php if($country_code == 'YU') echo ' selected'; ?>>SERBIA AND MONTENEGRO</option>
<option value="SC"<?php if($country_code == 'SC') echo ' selected'; ?>>SEYCHELLES</option>
<option value="SL"<?php if($country_code == 'SL') echo ' selected'; ?>>SIERRA LEONE</option>
<option value="SG"<?php if($country_code == 'SG') echo ' selected'; ?>>SINGAPORE</option>
<option value="SK"<?php if($country_code == 'SK') echo ' selected'; ?>>SLOVAKIA</option>
<option value="SI"<?php if($country_code == 'SI') echo ' selected'; ?>>SLOVENIA</option>
<option value="SB"<?php if($country_code == 'SB') echo ' selected'; ?>>SOLOMON ISLANDS</option>
<option value="SO"<?php if($country_code == 'SO') echo ' selected'; ?>>SOMALIA</option>
<option value="ZA"<?php if($country_code == 'ZA') echo ' selected'; ?>>SOUTH AFRICA</option>
<option value="GS"<?php if($country_code == 'GS') echo ' selected'; ?>>SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS</option>
<option value="ES"<?php if($country_code == 'ES') echo ' selected'; ?>>SPAIN</option>
<option value="LK"<?php if($country_code == 'LK') echo ' selected'; ?>>SRI LANKA</option>
<option value="SD"<?php if($country_code == 'SD') echo ' selected'; ?>>SUDAN</option>
<option value="SR"<?php if($country_code == 'SR') echo ' selected'; ?>>SURINAME</option>
<option value="SJ"<?php if($country_code == 'SJ') echo ' selected'; ?>>SVALBARD AND JAN MAYEN</option>
<option value="SZ"<?php if($country_code == 'SZ') echo ' selected'; ?>>SWAZILAND</option>
<option value="SE"<?php if($country_code == 'SE') echo ' selected'; ?>>SWEDEN</option>
<option value="CH"<?php if($country_code == 'CH') echo ' selected'; ?>>SWITZERLAND</option>
<option value="SY"<?php if($country_code == 'SY') echo ' selected'; ?>>SYRIAN ARAB REPUBLIC</option>
<option value="TW"<?php if($country_code == 'TW') echo ' selected'; ?>>TAIWAN, PROVINCE OF CHINA</option>
<option value="TJ"<?php if($country_code == 'TJ') echo ' selected'; ?>>TAJIKISTAN</option>
<option value="TZ"<?php if($country_code == 'TZ') echo ' selected'; ?>>TANZANIA, UNITED REPUBLIC OF</option>
<option value="TH"<?php if($country_code == 'TH') echo ' selected'; ?>>THAILAND</option>
<option value="TG"<?php if($country_code == 'TG') echo ' selected'; ?>>TOGO</option>
<option value="TK"<?php if($country_code == 'TK') echo ' selected'; ?>>TOKELAU</option>
<option value="TO"<?php if($country_code == 'TO') echo ' selected'; ?>>TONGA</option>
<option value="TT"<?php if($country_code == 'TT') echo ' selected'; ?>>TRINIDAD AND TOBAGO</option>
<option value="TN"<?php if($country_code == 'TN') echo ' selected'; ?>>TUNISIA</option>
<option value="TR"<?php if($country_code == 'TR') echo ' selected'; ?>>TURKEY</option>
<option value="TM"<?php if($country_code == 'TM') echo ' selected'; ?>>TURKMENISTAN</option>
<option value="TC"<?php if($country_code == 'TC') echo ' selected'; ?>>TURKS AND CAICOS ISLANDS</option>
<option value="TV"<?php if($country_code == 'TV') echo ' selected'; ?>>TUVALU</option>
<option value="UG"<?php if($country_code == 'UG') echo ' selected'; ?>>UGANDA</option>
<option value="UA"<?php if($country_code == 'UA') echo ' selected'; ?>>UKRAINE</option>
<option value="AE"<?php if($country_code == 'AE') echo ' selected'; ?>>UNITED ARAB EMIRATES</option>
<option value="UK"<?php if($country_code == 'UK') echo ' selected'; ?>>UNITED KINGDOM</option>
<option value="US"<?php if($country_code == 'US') echo ' selected'; ?>>UNITED STATES</option>
<option value="UM"<?php if($country_code == 'UM') echo ' selected'; ?>>UNITED STATES MINOR OUTLYING ISLANDS</option>
<option value="UY"<?php if($country_code == 'UY') echo ' selected'; ?>>URUGUAY</option>
<option value="UZ"<?php if($country_code == 'UZ') echo ' selected'; ?>>UZBEKISTAN</option>
<option value="VU"<?php if($country_code == 'VU') echo ' selected'; ?>>VANUATU</option>
<option value="VE"<?php if($country_code == 'VE') echo ' selected'; ?>>VENEZUELA</option>
<option value="VN"<?php if($country_code == 'VN') echo ' selected'; ?>>VIET NAM</option>
<option value="VG"<?php if($country_code == 'VG') echo ' selected'; ?>>VIRGIN ISLANDS, BRITISH</option>
<option value="VI"<?php if($country_code == 'VI') echo ' selected'; ?>>VIRGIN ISLANDS, U.S.</option>
<option value="WF"<?php if($country_code == 'WF') echo ' selected'; ?>>WALLIS AND FUTUNA</option>
<option value="EH"<?php if($country_code == 'EH') echo ' selected'; ?>>WESTERN SAHARA</option>
<option value="YE"<?php if($country_code == 'YE') echo ' selected'; ?>>YEMEN</option>
<option value="ZM"<?php if($country_code == 'ZM') echo ' selected'; ?>>ZAMBIA</option>
<option value="ZW"<?php if($country_code == 'ZW') echo ' selected'; ?>>ZIMBABWE</option>
</select>

 


Do you like this article? Share it with others by clicking the social media buttons below. We will write more articles related to this topic.