?
Solved

Getting the country name of the client machine

Posted on 1999-12-22
22
Medium Priority
?
555 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:kamalranjan
  • 9
  • 8
  • 5
22 Comments
 
LVL 2

Accepted Solution

by:
meming earned 100 total points
ID: 2300171
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 2300775
This is of course strictly server based unless you use java applets on the client communicating with the server.

Michel
0
 
LVL 2

Author Comment

by:kamalranjan
ID: 2302417
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 2302821
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
 
LVL 2

Author Comment

by:kamalranjan
ID: 2302930
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
 
LVL 2

Author Comment

by:kamalranjan
ID: 2302937
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 2303036
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
 
LVL 2

Author Comment

by:kamalranjan
ID: 2303130
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 2303224
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 2303226
PS: Meming's answer is in my opinion still valid...

0
 
LVL 2

Expert Comment

by:meming
ID: 2303471
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 2303490
Hehe - we are into apologising for language ideosyncrazies today huh?
I kinda liked my associative array ;-)
Michel
0
 
LVL 2

Expert Comment

by:meming
ID: 2303659
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
 
LVL 2

Author Comment

by:kamalranjan
ID: 2304715
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
 
LVL 2

Expert Comment

by:meming
ID: 2304785
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
 
LVL 2

Author Comment

by:kamalranjan
ID: 2304825
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
 
LVL 2

Expert Comment

by:meming
ID: 2304833
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 2305285
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
 
LVL 2

Author Comment

by:kamalranjan
ID: 2311494
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 2311857
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
 
LVL 2

Author Comment

by:kamalranjan
ID: 2312055
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 2312129
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

593 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