Getting the country name of the client machine

Hi,

I am building a site using ASP's as server end scripting and javascript as client end scripting whereby i am supposed to check whether the request given by a client browser has come from which country and accordingly the page will be generated for the client's machine. I guess whenever a client connects to internet through whichever ISP, he gets an unique IP assigned by his ISP, who have some specific range of IP's. The IP also varies whenever the client connects to internet. I want to know how is the range of IP's divided among ISP's of various countries(specifically, as my functionality requires that) or continents whatever may be the case, so that i can accordingly map the country with the IP. Any help is highly Appreciated.

Kamal Ranjan
LVL 2
kamalranjanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

memingCommented:
With the help of DNS, you should be able to nslookup the domain name of the the client IP. Most of the Web access logging packages keep a top level domain name to country mapping.

ie.  192.208.*.1 --nslookup--> *.edu --> United States

Request.ServerVariables( "REMOTE_HOST" )
hostname = ReverseDNSLookup ("255.255.255.0")
country = myHostToCountryFunc(hostname)

A vendor of DNS lookup component:
http://www.serverobjects.com/products.htm#aspdns

A list of domain-country mapping:
http://www.statslab.cam.ac.uk/~sret1/analog/domains.tab

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michel PlungjanIT ExpertCommented:
This is of course strictly server based unless you use java applets on the client communicating with the server.

Michel
0
kamalranjanAuthor Commented:
Meming, though the two links provided by you doesn't solve the purpose as they can recognize the country name based on domain name whereas i am talking about the client location and not the Web Site location. The function name myHostToCountryFunc() seems to return the country name. But where do i get this function and how can i use it in my ASP page.

Kamal Ranjan
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Michel PlungjanIT ExpertCommented:
Kamal: the IP address of the visitor will show where they are from on a reverse lookup. It is not the web site, but the ISP's location you get (or not, in case of com, org or int).

Michel
0
kamalranjanAuthor Commented:
Hi Michael,

I knew without you the problem wont get solved and that is why i posted it on javascript area though it is not merely a javascript problem. But i don't know the reverse lookup procedure for the IP which as you have said will give the ISP location. As such that's what i exactly want provided the user and the ISP are from same location(atleast from the same country as per my concern). I am doing a server end scripting in ASP and i can get the IP of the user out there but how to perform a reverse lookup.

Kamal Ranjan
0
kamalranjanAuthor Commented:
Hi Michael,

I knew without you the problem wont get solved and that is why i posted it on javascript area though it is not merely a javascript problem. But i don't know the reverse lookup procedure for the IP which as you have said will give the ISP location. As such that's what i exactly want provided the user and the ISP are from same location(atleast from the same country as per my concern). I am doing a server end scripting in ASP and i can get the IP of the user out there but how to perform a reverse lookup.

Kamal Ranjan
0
Michel PlungjanIT ExpertCommented:
Thanks for the confidence ;-)  However this IS a server side question and is more or less answered by meming.

You need a program to be called from asp installed on your server.

Meming posted one - here is here is a post from deja also mentioning this:

"This is the correct solution, but in many cases the host name cannot be
found by the server.  I use a component called ASPDNS from
www.serverobjects.com.  I then do a reverse DNS lookup on the IP address -
Request.ServerVariables("REMOTE_HOST").

This would then translate the IP address into a host name xxx.demon.co.uk

It cant be relied on though as sometimes RDNS fails, plus some UK users for example have ISPs which use .com (Compuserve) or .net (UUNet) so you cannot relably detect the country 100% of the time."

Alternatively (and better in my opinion) is to ask.

I am danish but live in holland using the internation version of the english netscape. What page will you show me in what language?

If you provide localised data, I probably want a page about Holland, but perhaps I want it in English????

If you just want to know where they are form, use a stats server like nedstat - they will show you a breakdown on origin.

Michel
0
kamalranjanAuthor Commented:
Hi,

The links in www.serverobjects.com site are ASPDNS which gives you the gives the list of extension of domain name with the respective country and another facility to convert an IP to domain name. If this is reverse lookup then that's not exactly what i need. B'coz i don't care what his machine name is. I just want to get where he(user accessing my site) is from. Could you pl help me in this.

Thankx
Kamal Ranjan
0
Michel PlungjanIT ExpertCommented:
I think you misunderstand.

Here is the documentation for the serverobject aspDNS :

You are not interested in DNSLookup but you want this:

2.ReverseDNSLookup(IPAddress - string)
e.g. ReverseDNSLookup ("255.255.255.0")
If the IP address is found in the DNS the return value is the hostname.

My hostname right now is
something.something.nl
meaning I sit in holland so you would do something like this (sorry for the javascript, I am not fluent in asp):

<script runat=server language=javascript">
dns = ReverseDNSLookup (Request.ServerVariables("Remote_Host"))

// following list is from
// http://www.statslab.cam.ac.uk/~sret1/analog/domains.tab
cc= new Array()
cc["ad"] = "Andorra";
cc["ae"]= " United Arab Emirates";
..
..
..
cc["nf"] = "Norfolk Island";
cc["ng"] = "Nigeria";
cc["ni"] = "Nicaragua";
cc["nl"] = "Netherlands";
..
..
cc["zw"] = "Zimbabwe";

country = dns.substring(dns.lastIndexOf('.'))
Response.write('We welcome our visitor from' + cc[country]);
</SCRIPT>

0
Michel PlungjanIT ExpertCommented:
PS: Meming's answer is in my opinion still valid...

0
memingCommented:
Mplungian, thanks for doing all the explaination for me. I haven't been able to following my questions often for I have started my holidays.

Being the type who can't code in all languages without reference books, I can only provide the following semi-pseudo code for ASP at the moment( sorry if it's too javaish):

clientIP = Request.ServerVariables( "REMOTE_HOST" )
clientHostname = ReverseDNSLookup (clientIP)
country = myHostToCountryFunc(hostname)

function myHostToCountryFunc(domainFileName, clientHostname)

domainFileString = readInFileString(domainFileName)
' sorry. can't give details on this readInFileString function. hope this won't be a problem to you ;)

  for each line in domainFileString
    tokens = line.split(" ") 'or asp equivalent
    countryDomain = tokens[0] 'ie zw
    countryName = tokens(1) 'ie Zimbabwe
    if clientHostname.endsWith("."&countryDomain) ' or asp equivalent
      myHostToCountryFunc = countryDomain
    end if
  end for

end function

If the call is:
countryString = myHostToCountryFunc("domain.tab","dummyserver.dummydomain.zw")
The countryString will be set to:
"Zimbabwe"

Then it's time to say
"Where do you want to go today, my friend from Zimbabwe?

0
Michel PlungjanIT ExpertCommented:
Hehe - we are into apologising for language ideosyncrazies today huh?
I kinda liked my associative array ;-)
Michel
0
memingCommented:
After seeing my JFC/Swing program runs into 20mg range in memory, I have become much more careful about code efficiency. OO is cool, but not without a price (particularly when the price is set by MS ;\).
0
kamalranjanAuthor Commented:
I reapeat the links in www.serverobjects.com site are ASPDNS which gives you the gives the list of extension of domain name with the respective country and another facility to convert an IP to domain name. If this is reverse lookup then that's not exactly what i need. B'coz i don't care what his machine name is. I just want to get where he(user accessing my site) is from.

For eg. lets say you(Meming or Micheal) are accessing a page in www.experts-exchange.com then in that page i want to display where you are accessing this page. Here there isn't any concept of sites, which should be mapped to the .nl or .au strings to get country. Here the user(Meming or Micheal) may be sitting on a computer in Holland or lets say in US, then that country where he is currently accessing the page is to be displayed. I hope this time i am clear what i want.

Thankx
Kamal Ranjan
0
memingCommented:
Kamalranjan, the ONLY way that you can figure out the location/country of the client machine is from the domain part of its hostname, whether you care or not.

I think your original question is clearer than your comments. To "map the country with the IP", you would have to use domain names, but not the "IP ranges". It's just how it works. Since IP/Domainname only reflects the logical structure of computer networks, the realtime physical locations of the computers are unknown without manual tracing or a GPS.

Since ASP can't always get the hostname of the client machine, IP addresses would have to be resolved into hostnames. That's why ASPDNS is in the picture. It's doesn't "exactly" do what you are requesting, but is what you would have to do to get the work done.

Let me put things in order for you again (as shown in my 2nd last comment):
1) ASP gets client IP (ie 200.2.3.4)
2) use ASPDNS to convert IP to domainname (ie my.co.zw)
3) use "zw" as the key to find its corresponding country name in the mapping file (ie Zimbabwe)
4) Now you can do exactly what you want -- displaying "Zimbabwe" in your HTML page

Shall we call it "Client IP to Country Name Translation".

(Having read your last comment several times, it's my turn to get confused now. It seems that you understood both of the problem and my answer, but just couldn't make the connection between the two. Hope the above explaination helps this time.)
0
kamalranjanAuthor Commented:
meming, i feel the client IP is assigned dynamically by the ISP while connecting to internet and this is assigned from a set of free IP's that the ISP must be having. If this is the case then these IP's should not have any domain name. Now as per your statements how will you get the second point done ie.
use ASPDNS to convert IP to domainname (ie my.co.zw)

I am sorry if i am unclear about certain things but i really can't understand as explained above as to how any domain name is fixed for a dynamically allocated IP address to the user by an ISP while connecting to internet for surfing.

Kamal Ranjan
0
memingCommented:
My ISP actually assigns me an IP that can be resolved into a valid hostname. And I know AOL does the same. Don't know whether every ISP does it. You do have a point that I didn't consider. I believe this is one of the cases that an IP can't be resolved at all. I don't think you can get around this one. DNS has limitations.

As you might have seen, http://www.serverobjects.com/comp/aspdns.htm gives an example of the usage of the ASPDNS component in ASP (I modified it to do reverse lookup):

<%
Set DNSLook = Server.CreateObject("AspDNS.Lookup")
hostname = DNSLook.ReverseDNSLookup ("255.255.255.0")
Response.Write ("<b>The hostname of 255.255.255.0 is " & hostname & "</b>")
%>
0
Michel PlungjanIT ExpertCommented:
My ip address resolves to something like
proxy.demon.nl because I go via the dutch proxy
it would be somethingelse.demon.nl if I didn't and if I chose to use the UK proxy for fun, the remote host I would present to your site would resolve to proxy.demon.co.uk

If what you want makes it possible for you to say to me
"Hi and welcome, visitor from Holland" when I visit your site, then we have fully understood what you want.

Michel
0
kamalranjanAuthor Commented:
Sorry for not being in touch for past four days but i think you R on right track michel. The word holland in the Welcome line is coming dynamically but we have to find that out how. Pl go ahead.

Kamal Ranjan
0
Michel PlungjanIT ExpertCommented:
If we are on the right track, please try out meming's suggestion, download and install the ASPDNS thingy and follow instructions:


cc= new Array()
cc["ad"] = "Andorra";
cc["ae"]= " United Arab Emirates";
...
...
...
cc["nf"] = "Norfolk Island";
cc["ng"] = "Nigeria";
cc["ni"] = "Nicaragua";
cc["nl"] = "Netherlands";
...
...
cc["zw"] = "Zimbabwe";

<% Set DNSLook = Server.CreateObject("AspDNS.Lookup")
   hostname = DNSLook.ReverseDNSLookup (Request.ServerVariables( "REMOTE_HOST" ) )
   Response.Write ("var dns = '" & hostname & "';")  %>

country = dns.substring(dns.lastIndexOf('.')+1);

document.write("Hi and welcome, visitor from " + cc[country]);

Michel
0
kamalranjanAuthor Commented:
That may work bcoz your proxy might be already having a unique IP assigned and thus a domain name too but as i already told same may not be the case with many internet surfers as there may not be a unique IP address assigned to their proxy machine or individual standalone machine as the case may be. In this case DNS resolution will give the physical name assigned to that machine or may be just nothing. And i feel most of the surfers are getting the IP dynamically by their ISP while they connect to the internet atleast here in India(as i am in India). For that case how will ASPDNS help?

Kamal Ranjan
0
Michel PlungjanIT ExpertCommented:
Even if their isp gives them a temp ip address, the reverse lookup should as far as I know return the country of the isp. If so, I do not think many users will use an isp in another country than where they are from.

If it does not work, it does not work and then there is no way I know of that can help you unless you ask the user to tell you where they are from

Michel
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.