Solved

Determine country from IP

Posted on 2002-06-13
5
1,109 Views
Last Modified: 2011-09-20
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
Comment
Question by:pede
5 Comments
 
LVL 2

Expert Comment

by:freshman3k
Comment Utility
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
 
LVL 1

Author Comment

by:pede
Comment Utility
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
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
Comment Utility
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
 
LVL 1

Author Comment

by:pede
Comment Utility
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
 
LVL 26

Expert Comment

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

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now