Obtaining the users IP Address

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Hi all

I am trying to obtain the user IP address, to determine what country that they are comming from. The purpose of this information is to redirect the user to a particular page that is configured for the individual country.

Any suggestions on how to achieve this? So far I have managed to obtain the IP address of the server, but not the other way around.

Mike55.
 
mike55 said:
Hi all

I am trying to obtain the user IP address, to determine what country that they are comming from. The purpose of this information is to redirect the user to a particular page that is configured for the individual country.

Any suggestions on how to achieve this? So far I have managed to obtain the IP address of the server, but not the other way around.

Mike55.

Request.UserHostAddress should do the trick.
 
Many Thanks for the reply. Here is the code that I have started using:
Code:
    Private Sub DetermineUsersIPAddress()
        'Response.Write(Request.ServerVariables("REMOTE_ADDR"))
        Dim strIp As String

        strIp = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
        If strIp = "" Then
            strIp = Request.ServerVariables("REMOTE_ADDR")
        End If

        Response.Write(strIp)
    End Sub

Now the next step is to find the country that the IP address belongs to.

Mike55.
 
Visual Basic:
'.Net 2.0
Dim host As String
host = System.Net.Dns.GetHostEntry(Request.UserHostAddress).HostName

'.Net 1
Dim host As String
host = System.Net.Dns.GetHostByAddress(Request.UserHostAddress)

The variable host should contain the fully qualified domain name - the country might be obtainable from parsing this out. Be aware this is not 100% (probably nowhere near in fact.)

You might find a better solution is to run a WHOIS query agains the IP address -
http://www.vbdotnetheaven.com/Uploa...rticleID=63b59267-40fc-40d4-b724-df0d77dbef97 has a sample that might help.
 
Back
Top