• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Coding Still

  • Home
  • About

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.

public static string GetCurrentIpAddress(HttpRequest request)
{
    string ipAddress = "";
 
    if (string.IsNullOrEmpty(ipAddress)) { ipAddress = request.ServerVariables["HTTP_CLIENT_IP"]; }
    if (string.IsNullOrEmpty(ipAddress)) { ipAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"]; }
    if (string.IsNullOrEmpty(ipAddress)) { ipAddress = request.ServerVariables["HTTP_X_FORWARDED"]; }
    if (string.IsNullOrEmpty(ipAddress)) { ipAddress = request.ServerVariables["HTTP_X_REAL_IP"]; }
    if (string.IsNullOrEmpty(ipAddress)) { ipAddress = request.ServerVariables["HTTP_X_CLUSTER_CLIENT_IP"]; }
    if (string.IsNullOrEmpty(ipAddress)) { ipAddress = request.ServerVariables["HTTP_FORWARDED_FOR"]; }
    if (string.IsNullOrEmpty(ipAddress)) { ipAddress = request.ServerVariables["HTTP_FORWARDED"]; }
    if (string.IsNullOrEmpty(ipAddress)) { ipAddress = request.ServerVariables["REMOTE_ADDR"]; }
 
    return ipAddress;
}

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.

public static string GetIp(string HostName)
{
    try
    {
        System.Net.IPHostEntry ips;
        ips = System.Net.Dns.GetHostEntry(HostName);
        return ips.AddressList[1].ToString();
    }
    catch (Exception ex)
    {
        // Handle the error
    }
}

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 Tagged With: Client info detection, PHP

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Categories

  • .NET Development
  • ASP.NET
  • Databases
  • Fun
  • IIS
  • JavaScript
  • Web Development

Tags

.NET Core Android ANTLR ASP.NET Ajax ASP.NET Core ASP.NET MVC ASP.NET Web Forms AWS Bouncy Castle Chartjs cli Client info detection Comic Continuous integration CSS Data backup Date handling Firebase Firefox addons Github HigLabo HTML5 Image manipulation jQuery JWT MySQL Nodejs Nuget OAuth Objectionjs OOP openssl Oracle ORM PHP Regular expressions SEO Social media SQL SQL Server UI/UX Url rewriting Videos Visual Studio Web design

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Secondary Sidebar

Archives

  • July 2020
  • March 2020
  • August 2019
  • December 2018
  • November 2018
  • February 2018
  • August 2016
  • June 2016
  • May 2016
  • February 2016
  • January 2016
  • August 2015
  • July 2015
  • October 2014
  • July 2014
  • November 2013
  • April 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • May 2012
  • February 2012
  • December 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010

Footer

Recent Posts

  • Anatomy of an Objection.js model
  • Check your RSA private and public keys
  • Round functions on the Nth digit
  • Send FCM Notifications in C#
  • Jwt Manager
  • Things around the web #5
  • Query JSON data as relational in MySQL
  • Create and sign JWT token with RS256 using the private key
  • Drop all database objects in Oracle
  • Create and deploy a Nuget package

Latest tweets

  • Things I Wished More Developers Knew About Databases https://t.co/h4gfq6NJgo #softwaredevelopment #databases May 3, 2020 12:52
  • How a Few Lines of Code Broke Lots of Packages https://t.co/p7ZSiLY5ca #javascript May 3, 2020 12:48
  • Can someone steal my IP address and use it as their own? https://t.co/HoQ7Z3BG69 January 24, 2020 13:27
  • Organizational complexity is the best predictor of bugs in a software module https://t.co/aUYn9hD4oa #softwaredevelopment January 13, 2020 08:24
  • Capitalism for people. Socialism for companies? https://t.co/zgUoPcqnix January 5, 2020 07:46
  • http://twitter.com/codingstill

Misc Links

  • George Liatsos Blog
  • Plethora Themes
  • C# / VB Converter
  • Higlabo: .NET library for mail, DropBox, Twitter & more

Connect with me

  • GitHub
  • LinkedIn
  • RSS
  • Twitter
  • Stack Overflow

Copyright © 2021 · eleven40 Pro on Genesis Framework · WordPress · Log in