Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserHostAddress gives wrong IPs

I collect statistics on IP addresses from where users visit my site and I have noticed what there are only two IP addresses presented, 172.16.16.1 and 172.16.16.248. The property I use to determine IP address is

Request.UserHostAddress

What could be a reason of IP address information losing? All the users are from around the world, so they cann't be behind only two proxies.

like image 383
Alexander Prokofyev Avatar asked Oct 14 '08 09:10

Alexander Prokofyev


1 Answers

You might want to something like this;

string SourceIP = String.IsNullOrEmpty(Request.ServerVariables["HTTP_X_FORWARDED_FOR"]) ? Request.ServerVariables["REMOTE_ADDR"] : Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(",")[0];

The HTTP_X_FORWARDED_FOR header gets the IP address behind proxy servers.

See this page that explains why in more detail; Getting The Real IP of your Users

like image 124
Dave Anderson Avatar answered Sep 18 '22 13:09

Dave Anderson