Occasionally, it’s handy to know what your public IP address, which is the IP address assigned to you by your ISP.  Most home users will have a Dynamic IP address, meaning they won’t always have the same IP address when connected to the internet.  People may use this public IP address for all sorts of things, such as remote connections (e.g., for tech support), gaming, or just to see if their address has changed.

There are a few websites out there that will show you what your IP address is, and by far, the best one I found was http://www.whatismyip.org/.  This was a favourite of mine, simply because all it did was show your your IP address… nothing else.

I found that the site has become unreliable.  Quite often it does not work from the office, and I have to use one of the other sites to check my address.  whatismyip.org was always super quick because all it had to load was your IP address, so at most, a 15 byte page.  Because it had become unreliable, I decided to write my own page that mimics the functionality.

The first one I’ve written in ASP.NET, as this is what I primarily program in, and I have my own site to which I can easily add a new page.  So, without further ado, here’s my ASP.NET version of What Is My IP.

The code for the page is as follows:

<@ WebHandler Language="VB" Class="ip" %>
Imports System
Imports System.Web

Public Class ip : Implements IHttpHandler
  Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    context.Response.ContentType = "text/plain"

    If context.Request.UserHostAddress Is Nothing OrElse context.Request.UserHostAddress = "" Then
      context.Response.Write(context.Request.ServerVariables("REMOTE_ADDR"))
    Else
      context.Response.Write(context.Request.UserHostAddress)
    End If
  End Sub

  Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    Get
      Return False
    End Get
  End Property
End Class

Now, some will notice this is an ASHX page, which is an ASP.NET generic handler.  The reason I chose this filetype was simply because I wanted the resulting output to be as small as possible, and an ashx file is perfect for this.

There are two things going on here.  First, context.Response.ContentType = “text/plain” sets the content type of the page to a plain text document, so there is no html fluff bloating the page.  The other thing is to get the IP address, and I use one of two ways to get it.  context.Request.UserHostAddress or context.Request.ServerVariables(“REMOTE_ADDR”).  I chose to try both ways, simply because both return the public IP address, and if one fails, then you get a second chance at showing it.  I’m really not sure which one works more than the other, and I will do some logging on this to determine which is more reliable.  Check back in a few weeks for an update.

Now, because I’d decided to write about this page on my blog, and my blog is using WordPress (and hosted on a server that handles php), I decided to have a look at the same thing in PHP.  I’ve been meaning to learn more about PHP, so this was a good opportunity.  So, the resulting page was my PHP version of What Is My IP.

The code for the page is as follows:

<?php
  header("content-type: text/plain");
  echo $_SERVER['REMOTE_ADDR'];
?>

It’s glaringly obvious that the PHP version is a lot less lines of code for the same result (mind out I could cut out 4 lines of .net code if I took out the if/else statement).  This has made me even more keen on learning PHP.

There you have it.  At least another couple of places you can check your IP address if you need to know.  It will also be interesting to see if this page starts ranking for “What is my ip” in google.  As always, hopefully someone finds this useful.