|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
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. 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. IP Number = 16777216*w + 65536*x + 256*y + z (1)
where IP Address = w.x.y.z
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
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. "3401056256","3401400319","MY","MALAYSIA"
How do I use this DatabaseCSV File FormatThe CSV file contains four fields:
"0033996344","0033996351","GB","GBR","UNITED KINGDOM" "0050331648","0083886079","US","USA","UNITED STATES" "0094585424","0094585439","SE","SWE","SWEDEN"
Downloads CSV DatabaseDownload the latest IP-to-Country Database (Last updated on July 21 2008) http://ip-to-country.webhosting.info/node/view/6Convert 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
The detail adds. http://www.aspxcode.net/How-do-IPAddress-to-NameCountry.aspx
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||