5,446,542 members and growing! (19,260 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto License: The Code Project Open License (CPOL)

How do Convert IP Address to Country Name

By aspxCode

How do I retrieve the Country Name and Country Code from the IP Number?
Javascript, CSS, HTML, XHTML, Ajax, ASP, ASP.NET

Posted: 4 Aug 2008
Updated: 14 Aug 2008
Views: 12,986
Bookmarked: 95 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
35 votes for this Article.
Popularity: 6.78 Rating: 4.39 out of 5
0 votes, 0.0%
1
1 vote, 2.9%
2
1 vote, 2.9%
3
14 votes, 40.0%
4
19 votes, 54.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

How to retrieve a visitor's IP address?

Every visitor to your site or web application has an IP address. It is quite handy to be able to get that address. It can be used for security logging, or perhaps tracing. It can also be used to determine where they are in the world, or at least where their ISP is.

The difficulty is when they're behind a proxy of some sort, you only see the IP address of the proxy server. So, here are the code snippets in ASP.NET that first check for an IP addresses that's forwarded from behind a proxy, and if there's none then just get the IP address. here's the same IP retriever with proxy detection in .NET, but in VB.NET

Dim nowip As String 
   nowip = Request.ServerVariables("HTTP_X_FORWARDED_FOR") 
   If nowip = "" Then
       nowip = Request.ServerVariables("REMOTE_ADDR")
   End If

How do I convert a IP Address to a IP Number?

IP address (IPV4) is divided into 4 sub-blocks. Each sub-block has a different weight number each powered by 256. IP number is being used in the database because it is efficient to search between a range of number in database.

Beginning IP number and Ending IP Number are calculated based on following formula:

IP Number = 16777216*w + 65536*x + 256*y + z (1)

where

IP Address = w.x.y.z



For example, if IP address is "202.186.13.4", then its IP Number "3401190660" is based on the formula (1).

IP Address = 202.186.13.4

So, w = 202, x = 186, y = 13 and z = 4

IP Number = 16777216*202 + 65536*186 + 256*13 + 4
= 3388997632 + 12189696 + 3328 + 4
= 3401190660



To reverse IP number to IP address,

w = int ( IP Number / 16777216 ) % 256
x = int ( IP Number / 65536 ) % 256
y = int ( IP Number / 256 ) % 256
z = int ( IP Number ) % 256
where
%

is the mod operator and

int

returns the integer part of the division.

How do I retrieve the Country Name and Country Code from the IP Number?

Search the IP-Country database to match a unique record that has the IP Number fits between Beginning IP Number and Ending IP Number.

For example, IP Address "202.186.13.4" is equivalent to IP Number "3401190660". It belongs to the following record in the database because it is between the beginning and the ending of IP number.

"3401056256","3401400319","MY","MALAYSIA"



From the recordset, the Country Name is Malaysia and Country Code is MY.

http://www.ip2location.com/faqs-ip-country.aspx

How do I use this Database

CSV File Format

The CSV file contains four fields:

  • Begining of IP address range
  • Ending of IP address range
  • Two-character country code based on ISO 3166
  • Three-character country code based on ISO 3166
  • Country name based on ISO 3166
"0033996344","0033996351","GB","GBR","UNITED KINGDOM"
"0050331648","0083886079","US","USA","UNITED STATES"
"0094585424","0094585439","SE","SWE","SWEDEN"
FIELD DATA TYPE FIELD DESCRIPTION
IP_FROM NUMERICAL (DOUBLE) Beginning of IP address range.
IP_TO NUMERICAL (DOUBLE) Ending of IP address range.
COUNTRY_CODE2 CHAR(2) Two-character country code based on ISO 3166.
COUNTRY_CODE3 CHAR(3) Three-character country code based on ISO 3166.
COUNTRY_NAME VARCHAR(50) Country name based on ISO 3166

Downloads CSV Database

Download the latest IP-to-Country Database (Last updated on July 21 2008)

http://ip-to-country.webhosting.info/node/view/6

Convert to Access Database



How-do-IPAddress-to-NameCountry.aspx.vb (Code-Behind)

            Partial Class How_to_Convert_IP_Address_Country
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object,
        ByVal e As System.EventArgs) Handles Me.Load
        Dim nowip As String
        nowip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
        If nowip = "" Then
            nowip = Request.ServerVariables("REMOTE_ADDR")
        End If

        If txtIPAddress.Text = "" Then
            txtIPAddress.Text = nowip
        End If
        lblError.Text = ""
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object,
        ByVal e As System.EventArgs) Handles Button1.Click
        On Error GoTo HandleError

        Dim dottedip As String
        Dim Dot2LongIP As Double
        Dim PrevPos As Double
        Dim pos As Double
        Dim num As Double

        dottedip = txtIPAddress.Text

            For i = 1 To 4
                pos = InStr(PrevPos + 1, dottedip, ".", 1)
                If i = 4 Then
                    pos = Len(dottedip) + 1
                End If
                num = Int(Mid(dottedip, PrevPos + 1, pos - PrevPos - 1))
                PrevPos = pos
                Dot2LongIP = ((num Mod 256) * (256 ^ (4 - i))) + Dot2LongIP
            Next

        txtIPNumber.Text = Dot2LongIP

HandleError:
        lblError.Text = Err.Description
    End Sub
End Class

How-to-Convert-IP-Address-Country.aspx (Output Result)

How-to-Convert-IP-Address-Country.aspx

check-conutry.jpg

The detail adds.

http://www.aspxcode.net/How-do-IPAddress-to-NameCountry.aspx

http://www.aspxcode.net

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

aspxCode



Occupation: Web Developer
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralMe da este error.memberMember 17399448:32 28 Aug '08  
GeneralVery usefulmemberjsreekumar13:07 22 Aug '08  
GeneralCoolmemberashu fouzdar4:23 19 Aug '08  
GeneralWow!memberAbhilashAshok21:12 13 Aug '08  
GeneralVery nicememberPetros Amiridis0:00 12 Aug '08  
GeneralthanksmemberMember 404680223:47 11 Aug '08  
GeneralCity codes too?memberWayne Hetherington14:01 11 Aug '08  
GeneralInterestingmemberkornakar21:38 5 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Aug 2008
Editor: Sean Ewington
Copyright 2008 by aspxCode
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project