0

OK, so I tried implementing this, http://ipaddressextensions.codeplex.com/.

It is displaying the output as:-

127.0.0.1 RESERVED ZZ

What on earth is this "RESERVED" and "ZZ"? It should be displayed as "INDIA IN".

The IP address is of the local host. All right, but what about the country name and country code? Why won't they display correctly? What do I need to change in my code?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Serenity
  • 4,968
  • 19
  • 65
  • 104
  • possible duplicate of [finding clients location in asp.net page.](http://stackoverflow.com/questions/1238180/finding-clients-location-in-asp-net-page) – John Saunders Sep 11 '10 at 09:29

3 Answers3

11

127.0.0.1 is a reserved IP address which stands for localhost (the computer that the code is running on). This is a "valid" IP address for every computer that runs TCP/IP.

IP lookups need a real IP address in order to work correctly.

In regards to the country code - it is probably a 2 character country ISO code (see here), so you need to translate the code to the correct country. ZZ is not in the table, signifying no country.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • How do I get the country name displayed the way I want ? I know its the local host's IP address coz I am running my website on just that..running it on local host won't display the country name either ?? – Serenity Sep 11 '10 at 08:13
  • @happysoul - You will need to look it up in the 2 character ISO country codes table. See my updated answer. – Oded Sep 11 '10 at 08:15
  • err..what table? I just d/l that IPAddress Extension dll file off that website and included it in my project..there's no DB to d/l on that website..sry but I am new to all this so don't rly know how it all works..plz help..thnx – Serenity Sep 11 '10 at 08:19
  • @happysoul - did you look at the page I linked to? It has a listing of 2 character codes and the countries they belong to. You can use that in your application to convert codes to country names. – Oded Sep 11 '10 at 08:20
  • that's a long list..now do I need to create a table on my own or there's one available on internet ready to use?? btw isn't this code line retrieving the country code only "string iso3166TwoLetterCode = ipAddress.Iso3166TwoLetterCode(); // return value: US" ...I might be wrong but isn't it some sort of built in thing in the DLL that I downloaded , which is retrieving country code?? sry if its confusing – Serenity Sep 11 '10 at 08:26
  • 1
    @happysoul - probably `ipAddress.Country();`. You need to use an IP address other than `127.0.0.1` for this to work, however. – Oded Sep 11 '10 at 08:30
  • I thot ipAddress.Country() is returnig the country name and not code..I just added some blocks of code..could u plz check it and tell me if I am doing it correctly or is there a better way to do it ..thnx – Serenity Sep 11 '10 at 08:40
  • @happysoul - I don't know if this is the right way. It completely depends on how the site works and if those sites exist. – Oded Sep 11 '10 at 08:45
  • assume that they exist..would it be the correct way then..Also lets say there's are like 10-15 websites ..wouldn't it be bad programming to type 10 "if" blocks?? What else can I use to shorten the code?? – Serenity Sep 11 '10 at 08:47
  • sry for the many questions..just want to use the best approach..so asking..plz help..thnx – Serenity Sep 11 '10 at 09:00
  • @happysoul - consider asking new questions as... new questions ;) – Oded Sep 11 '10 at 09:27
  • but they are all related to this only..do I need to ask separate questions for every single query ? Won't the questions be closed then ? I once did this..gave link to my earlier ques coz wasnt getting no updated answers and the question got closed :( – Serenity Sep 11 '10 at 09:34
  • @happysoul - If you just give a link, then yes. If you are asking a _related_ question, then it shouldn't get closed. – Oded Sep 11 '10 at 09:52
4

127.0.0.1 is known as the loopback address and is what your system uses to talk to itself effectively. As a result, there is no country associated with that IP address... it's everywhere on every system.

If you put your code out onto the web you'd get more appropriate results as each visiting system would be using a public IP address to reach your server.

Lazarus
  • 41,906
  • 4
  • 43
  • 54
  • oh ok..Could you please just look at the code and tell me if it will display the country name alright ?? I mean is the code correct ? thnx – Serenity Sep 11 '10 at 08:16
  • You need to get the IP address of the user host (the system requesting the page) using `Request.UserHostAddress` that may well help. Otherwise the code is correct and I believe the IPAddressExtensions include all the data you'd need. – Lazarus Sep 11 '10 at 08:32
  • what about REMOTE_ADDR ?? Shouldn't use that either ?? btw "HTTP_X_FORWARDED_FOR" returns nothing ..dunno why is that – Serenity Sep 11 '10 at 08:37
  • so I assume IPAddressExtensions is using some DB and I don't need to create no table on my own ..is that so ? – Serenity Sep 11 '10 at 08:38
  • @Peter Mortensen : Thanks, you could have just made the edit, I wouldn't have minded :) – Lazarus Dec 01 '10 at 11:33
  • @Serentity : Apologies, I was away for a while and must have missed the comment alert. You could user REMOTE_ADDR but it's already captured in the Request object. And yes, I believe the library comes with the GeoIP data needed to correctly identify the visitors location (at least reasonably accurately). – Lazarus Dec 01 '10 at 11:37
2

When you are testing from home, both server and user are one (your PC). So you can't expect it to show the country as the IP address for IIS is a self address.

Your code seems to be fine. Also you can use a free ASP.NET supporting host to try your website online. There are many like HelioHost, 0000free, etc.

Regarding the edit part of your question, if you are using different versions of the site for each country then wouldn't be using a switch better.

switch(iso3166TwoLetterCode.ToUpper())
{
    case "IN" : Response.Redirect("www.mysite.in");
    case "FR" : Response.Redirect("www.mysite.fr");
    ...
    Default : Response.Redirect("www.mysite.in");
}

I think it does look neater.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
loxxy
  • 12,990
  • 2
  • 25
  • 56
  • thnx for answering..I ll just try the switch case ..oh a ques just came to my mind.. in switch case what if I need to check BOTH for "IN" and "in" ie both cases..how do I do that ? Do I need to write two separate cases or it can be done using one by using OR etc ?? – Serenity Sep 11 '10 at 09:29
  • You can use or operator only for boolean type variables not Strings. I would suggest using toupper or tolower....see the edit above. – loxxy Sep 11 '10 at 09:43
  • like this ?... if(iso3166TwoLetterCode.ToUpper()) {} ..what its gonna do ? check if its uppercase ?? then what abt lower case?? sry not rly getting how to implement – Serenity Sep 11 '10 at 09:48
  • 1
    When you use toUpper() you make sure that even if it was "in", it will be converted to "IN". That way you can check for both "in" & "IN" using a single case. – loxxy Sep 11 '10 at 09:51
  • yeah got it..just studied abt it off internet..thnx :) – Serenity Sep 11 '10 at 09:56