Home | Installing | Start Learning Silverlight | Learn ASP.NET
Standard Data Validation Navigation Login WebParts AJAX  Eatensions HTML ASP.NET CODE General
 
 
 
 
 
 
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.r />
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
This is a sample of how the CSV file is structured:
"0033996344","0033996351","GB","GBR","UNITED KINGDOM"
"0050331648","0083886079","US","USA","UNITED STATES"
"0094585424","0094585439","SE","SWE","SWEDEN"

You can import this data into any database by creating a table with the following fields:
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
 
IP Database
 
How do I retrieve visitor's IP address using ASP.NET ( VB Source Code )
 
How-to-Convert-IP-Address-Country.aspx (Source Code)
 
How-do-IPAddress-to-NameCountry.aspx.vb (Code-Behind)
 
How-to-Convert-IP-Address-Country.aspx (Output Result)
 
Download Source Code

eXTReMe Tracker
Valid CSS! Valid XHTML 1.0 Transitional
© 2008 asp net tutorial.net sample aspx code All Rights Reserved
This Website Develop With Visual Web Developer 2008 Express Edition / ASP.NET 3.5 & Microsoft SQL Server 2005 Database.
You may contact for aspx codes on improvement of our website in terms of accuracy and completeness at Email: aspxcodes@gmail.com Thank you for visiting.
asp net tutorial & aspx tutorial