• Home
  • About
  • Portfolio
  • Contact
  • PostsComments

Coding Still

Baby debugging steps...

  • .NET Development
  • Asp.net
  • Fun
  • IIS
  • Javascript
  • Web Design
You are here: Home / Asp.net / Detecting Client’s IP Address

Detecting Client’s IP Address

September 10, 2011 By _tasos Leave a Comment

It’s pretty common the need for a web application to be able to detect the I.P. address of a client. The I.P. address could be used either for statistic or authentication purposes.

Knowing the IP address you could easily get information about the location of the client. Using Max Mind’s GeoIP Country and GeoIP City you can extract this information. Max Mind offers also a non-paying solution (which is less accurate though).

You can also use the IP address to block multiple logins with the same credentials. This could be useful for sites that offer content to users that have paid a subscription.

The code to do so is really easy, all you need to do is check a bunch of server variables. The good thing is that those variables are not platform specific, so an equivalent code could also work with PHP.

Function ClientIpAddress(ByVal myRequest As HttpRequest)
    Dim myIP As String = ""
 
    If (myIP = "") Then myIP = myRequest.ServerVariables("HTTP_CLIENT_IP")
    If (myIP = "") Then myIP = myRequest.ServerVariables("HTTP_X_FORWARDED_FOR")
    If (myIP = "") Then myIP = myRequest.ServerVariables("HTTP_X_FORWARDED")
    If (myIP = "") Then myIP = myRequest.ServerVariables("HTTP_X_CLUSTER_CLIENT_IP")
    If (myIP = "") Then myIP = myRequest.ServerVariables("HTTP_FORWARDED_FOR")
    If (myIP = "") Then myIP = myRequest.ServerVariables("HTTP_FORWARDED")
    If (myIP = "") Then myIP = myRequest.ServerVariables("REMOTE_ADDR")
 
    Return myIP
End Function

Sometimes though we do not have the actual IP address but we have the host name of the ISP provider of the client. The host name can be easily resolved into an IP address easily. Using the System.Net.Dns.GetHostEntry function we get a list of all the IP addresses this hostname resolves to.

Function Host2Ip(ByVal HostName As String) As String
    Try
        Dim myIPs As System.Net.IPHostEntry
        myIPs = System.Net.Dns.GetHostEntry(HostName)
        Host2Ip = myIPs.AddressList(0).ToString()
    Catch ex As Exception
        'Handle the error
    End Try
End Function

Finally, although this approach works much better than just looking at REMOTE_ADDR, still it’s far from a full proof solution, simply because it relies on the HTTP header information which can be easily manipulated.

Filed Under: Asp.net, Featured Tagged With: client info detection, php

Speak Your Mind Cancel reply

*

*

CAPTCHA Image
Refresh Image

*

Popular Posts

export_excel

Export data to excel for non-latin characters

A common feature in applications is to export data in excel. With .NET it is pretty simple with the …
[Read More...]

html5brwsers

HTML5 form validation

HTML5 and ccs3 are a really loud buzz in web design for quite some time. The new features can be …
[Read More...]

delegates

Using generics to override static methods

I am currently working on a library that will be used in my company's new and biggest project. This …
[Read More...]

redirecting

Handling redirections, missing pages and server errors in asp.net

In a CMS application it is really common for each authors to add, move and delete pages. In a busy …
[Read More...]

asp-net-ajax

ASP.NET and Ajax. All about update panels, web methods, page methods and jQuery

In modern web applications is expected that the implementation is more JavaScript based, so that its …
[Read More...]

db_backup

Backup the data from your Sql Server

An often requirement in a CMS is the user to be able to backup the database of its CMS installation. …
[Read More...]

what_is_my_ip

Detecting Client’s IP Address

It's pretty common the need for a web application to be able to detect the I.P. address of a client. …
[Read More...]

jqueryaspnet

Asp.net: jQuery and ClientID

Every ASP.NET control has the ID property, which is not the HTML id attribute. The HTML id attribute …
[Read More...]

jSmooth

jSmooth – A jQuery plugin

jSmooth is a jQuery plugin which allows you to have a smooth scrolling effect when you are in the …
[Read More...]

antlr_logo

AntLR And Visual Studio

Here is a simple tutorial in which there the instructions you need in order to begin working with …
[Read More...]

Return to top of page

Powered by Wordpress. Hosted by Rackspace Cloud