Click here to Skip to main content
11,644,982 members (57,174 online)
Click here to Skip to main content

Finding Countries and Cities Using IP Address

, 27 Aug 2006 42K 1.7K 23
Rate this:
Please Sign up or sign in to vote.
A simple article on finding the names of countries and cities using IP address, using www.showmyip.com (Note: free for only 199 request/day).

Introduction

This article describes how to find the names of countries and cities using the IP address.

Using the code

The code is given below with explanations:

//this line is to check the clien ip address from the server itself
string IP = Request.ServerVariables["REMOTE_ADDR"];

//Initializing a new xml document object to begin reading the xml file returned
XmlDocument doc = new XmlDocument();

//NOTE: www.showmyip.com only allows 199 request/day 
//as a free call, for more requests u could register with them
/////////////////

//send the request to www.showmyip.com sending the ip as a query string
//and loads the xml file in the document object
doc.Load("http://www.showmyip.com/xml/?ip=" + IP);

//begin finding the country by tag name
XmlNodeList nodeLstCountry = doc.GetElementsByTagName("lookup_country");

////begin finding the city by tag name
XmlNodeList nodeLstCity = doc.GetElementsByTagName("lookup_city2");

//concatinating the result for show, u could also use it
//to save in data base
IP = "Country :" + nodeLstCountry[0].InnerText + 
     " --  City :" + nodeLstCity[0].InnerText + "" + IP;
Response.Write(IP);

//this is my header that I love
Page.Header.Title = "I am muslim before anything";

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

realhossam
Web Developer
Egypt Egypt
Asp.net Developer since 2004.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Prashant100198715-Oct-10 0:34
memberPrashant100198715-Oct-10 0:34 
GeneralDoesn't return city any more Pin
SpringsteenFan11-Feb-09 2:13
memberSpringsteenFan11-Feb-09 2:13 
GeneralRe: Doesn't return city any more Pin
realhossam11-Feb-09 7:12
memberrealhossam11-Feb-09 7:12 
GeneralRe: Doesn't return city any more Pin
niku200913-Jan-12 22:32
memberniku200913-Jan-12 22:32 
GeneralRe: Doesn't return city any more Pin
Pavan Navule21-Aug-13 23:41
memberPavan Navule21-Aug-13 23:41 
QuestionRetrieved IP is wrong Pin
Lilupa22-Sep-07 21:54
memberLilupa22-Sep-07 21:54 
AnswerRe: Retrieved IP is wrong Pin
realhossam25-Sep-07 4:29
memberrealhossam25-Sep-07 4:29 
GeneralExcellent Pin
Lilupa22-Sep-07 21:24
memberLilupa22-Sep-07 21:24 
GeneralInteresting... Pin
xCuenta9-Apr-07 10:01
memberxCuenta9-Apr-07 10:01 
GeneralRe: Interesting... Pin
realhossam10-Apr-07 10:09
memberrealhossam10-Apr-07 10:09 
GeneralCool ! Pin
Mohammed Badran3-Sep-06 14:37
memberMohammed Badran3-Sep-06 14:37 
GeneralRe: Cool ! Pin
realhossam3-Sep-06 20:27
memberrealhossam3-Sep-06 20:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 28 Aug 2006
Article Copyright 2006 by realhossam
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid