Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1122
  • Last Modified:

Determine country from IP

Hi, what is the smartest and fastest way to find out which country an IP address belongs to? Can DNS lookup tell you this somehow?
0
pede
Asked:
pede
1 Solution
 
freshman3kCommented:
Hello!

Try this piece of code:

function ExtractCountry(IPAddress:string):string;
var
    strHost:string;
    addr:longint;
    hostptr:PHOSTENT;
begin
    addr:=inet_addr(PChar(v));
    hostptr:=gethostbyaddr(@addr,SizeOf(addr),PF_INET);
     if hostptr<>nil then
      strHost:=hostptr^.h_name;

    result:=strHost;

 { If you pass address 194.176.45.2 it will give you back
mydynamicip.myserver.mycountry (or whatever). You can then analyze the string and extract everything from the last dot => country}

end;

You need to include the winsock unit

Here is a list of countries:

AD`Andorra
AE`United Arab Emirates
AF`Afghanistan
AG`Antigua and Barbuda
AI`Anguilla
AL`Albania
AM`Armenia
AN`Netherland Antilles
AO`Angola
AQ`Antarctica
AR`Argentina
AS`American Samoa
AT`Austria
AU`Australia
AW`Aruba
AZ`Azerbaidjan
BA`Bosnia-Herzegovina
BB`Barbados
BD`Banglades
BE`Belgium
BF`Burkina Faso
BG`Bulgaria
BH`Bahrain
BI`Burundi
BJ`Benin
BM`Bermuda
BN`Brunei Darussalam
BO`Bolivia
BR`Brazil
BS`Bahamas
BT`Buthan
BV`Bouvet Island
BW`Botswana
BY`Belarus
BZ`Belize
CA`Canada
CC`Cocos (Keeling) Islands
CF`Central African Republic
CG`Congo
CH`Switzerland
CI`Ivory Coast
CK`Cook Islands
CL`Chile
CM`Cameroon
CN`China
CO`Colombia
CR`Costa Rica
CS`Czechoslovakia
CU`Cuba
CV`Cape Verde
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
ES`Spain
ET`Ethiopia
FI`Finland
FJ`Fiji
FK`Falkland Islands (Malvinas)
FM`Micronesia
FO`Faroe Islands
FR`France
FX`France (European Territory)
GA`Gabon
GB`Great Britain (UK)
GD`Grenada
GE`Georgia
GH`Ghana
GI`Gibraltar
GL`Greenland
GP`Guadeloupe (French)
GQ`Equatorial Guinea
GF`Guyana (French)
GM`Gambia
GN`Guinea
GR`Greece
GT`Guatemala
GU`Guam (US)
GW`Guinea Bissau
GY`Guyana
HK`Hong Kong
HM`Heard and McDonald Islands
HN`Honduras
HR`Croatia
HT`Haiti
HU`Hungary
ID`Indonesia
IE`Ireland
IL`Israel
IN`India
IO`British Indian Ocean Territory
IQ`Iraq
IR`Iran
IS`Iceland
IT`Italy
JM`Jamaica
JO`Jordan
JP`Japan
KE`Kenya
KG`Kirgistan
KH`Cambodia
KI`Kiribati
KM`Comoros
KN`Saint Kitts Nevis Anguilla
KP`North Korea
KR`South Korea
KW`Kuwait
KY`Cayman Islands
KZ`Kazachstan
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`Moldavia
MG`Madagascar
MH`Marshall Islands
ML`Mali
MM`Myanmar
MN`Mongolia
MO`Macau
MP`Northern Mariana Islands
MQ`Martinique (French)
MR`Mauritania
MS`Montserrat
MT`Malta
MU`Mauritius
MV`Maldives
MW`Malawi
MX`Mexico
MY`Malaysia
MZ`Mozambique
NA`Namibia
NC`New Caledonia (French)
NE`Niger
NF`Norfolk Island
NG`Nigeria
NI`Nicaragua
NL`Netherlands
NO`Norway
NP`Nepal
NR`Nauru
NT`Neutral Zone
NU`Niue
NZ`New Zealand
OM`Oman
PA`Panama
PE`Peru
PF`Polynesia (French)
PG`Papua New
PH`Philippines
PK`Pakistan
PL`Poland
PM`Saint Pierre and Miquelon
PN`Pitcairn
PT`Portugal
PR`Puerto Rico (US)
PW`Palau
PY`Paraguay
QA`Qatar
RE`Reunion (French)
RO`Romania
RU`Russian Federation
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 Islands
SK`Slovak Republic
SL`Sierra Leone
SM`San Marino
SN`Senegal
SO`Somalia
SR`Suriname
ST`Saint Tome and Principe
SU`Soviet Union
SV`El Salvador
SY`Syria
SZ`Swaziland
TC`Turks and Caicos Islands
TD`Chad
TF`French Southern Territory
TG`Togo
TH`Thailand
TJ`Tadjikistan
TK`Tokelau
TM`Turkmenistan
TN`Tunisia
TO`Tonga
TP`East Timor
TR`Turkey
TT`Trinidad and Tobago
TV`Tuvalu
TW`Taiwan
TZ`Tanzania
UA`Ukraine
UG`Uganda
UK`United Kingdom
UM`US Minor Outlying Islands
US`United States
UY`Uruguay
UZ`Uzbekistan
VA`Vatican City State
VC`Saint Vincent and Grenadines
VE`Venezuela
VG`Virgin Islands (British)
VI`Virgin Islands (US)
VN`Vietnam
VU`Vanuatu
WF`Wallis and Futuna Islands
WS`Samoa
YE`Yemen
YU`Yugoslavia
ZA`South Africa
ZM`Zambia
ZR`Zaire
ZW`Zimbabwe
ARPA`Old style Arpanet
COM`US Commercial
EDU`US Educational
GOV`US Government
INT`International
MIL`US Military
NATO`Nato field
NET`Network
ORG`Non-Profit


Hope This Helps!
0
 
pedeAuthor Commented:
If you look up my IP you will get .com as extension, that doesnt tell you which country I come from. But still some websites can redirect me to a danish version of their site, which is correct.

So they must do something else, right?
0
 
intheCommented:
they do that by reading the http headers for your language settings.it is quite easy using a serverside language (php etc) but i think you can do this also with javascript .
0
 
pedeAuthor Commented:
Hi inthe, I didnt get a notification mail about your reply, but I just saw it now. Thanks for the reply, I havent tested it but it sounds very likely to be true.


0
 
EddieShipmanCommented:
FWIW, see this post:
http://www.delphipages.com/threads/thread.cfm?ID=78896&G=78652 and the one directly beneath it...

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now