Saturday, September 19, 2009

Getting The Real IP Of Your Users(Track IP Address)

I have already written on post how to track the Visior i am going to explore how to track the IP Address of Visitor/Client.

The Following code will get the IP Address of visitor/ client.

C# code to track IP Address

string ipaddress;

// Look for a proxy address first
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

// If there is no proxy, get the standard remote address
if (ipaddress == "" || ipaddress == null)

ipaddress = Request.ServerVariables["REMOTE_ADDR"];

VB Code to track IP Address

' Look for a proxy address first
Dim _ip As String = Request.ServerVariables("HTTP_X_FORWARDED_FOR")

' If there is no proxy, get the standard remote address
If (_ip = "" Or _ip.ToLower = "unknown") Then _
_ip = Request.ServerVariables("REMOTE_ADDR")

Explaination of above Code

The HTTP_X_FORWARDED_FOR variable is to check that there isn't a non-transparent proxy in the way.When users are behind any proxies or routers the REMOTE_ADDR returns the IP Address of the router and not the client user’s machine. Hence first we need to check HTTP_X_FORWARDED_FOR, since when client user is behind a proxy server his machine’s IP Address the Proxy Server’s IP Address is appended to the client machine’s IP Address. If there are multiple proxy servers the IP Addresses of all of them are appended to the client machine IP Address.

Hence we need to first check HTTP_X_FORWARDED_FOR and then REMOTE_ADDR.

Note: While running this application on your machine it will show IP Address since your client and server is the same machine. when you will run this application on live server it will display the right information.

Happy Programming

No comments:

Post a Comment

Ideal SQL Query For Handling Error & Transcation in MS SQL