views:

151

answers:

3

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..alright..but what about the country name and country code ? Why won't they display correctly ? What do I need to change in my code ?? Plz help I am cluless :/

+8  A: 

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
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
@happysoul - You will need to look it up in the 2 character ISO country codes table. See my updated answer.
Oded
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
@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
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
@happysoul - probably `ipAddress.Country();`. You need to use an IP address other than `127.0.0.1` for this to work, however.
Oded
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
@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
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
sry for the many questions..just want to use the best approach..so asking..plz help..thnx
Serenity
@happysoul - consider asking new questions as... new questions ;)
Oded
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
@happysoul - If you just give a link, then yes. If you are asking a _related_ question, then it shouldn't get closed.
Oded
okay..thanks...
Serenity
+3  A: 

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 ever 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
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
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
what about REMOTE_ADDR ?? Shouldn't use that either ?? btw "HTTP_X_FORWARDED_FOR" returns nothing ..dunno why is that
Serenity
so I assume IPAddressExtensions is using some DB and I don't need to create no table on my own ..is that so ?
Serenity
there ?????????
Serenity
+1  A: 

When you are testing from home, both server n user are one (your pc). So you cant 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)
{
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.

Edit:

switch(iso3166TwoLetterCode.ToUpper())
loxxy
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
You can use or operator only for boolean type variables not Strings. I would suggest using toupper or tolower....see the edit above.
loxxy
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
loxxy
yeah got it..just studied abt it off internet..thnx :)
Serenity