Library code snippets

Get the visitor's IP address

Whenever a browser sends a request for a page, it also sends a number of other headers to the script, containing information such as the browser type. It also includes information such as the visitors IP address. This can be particularly useful when creating an online security scan, or logging their IP address in an online forum.

There are two server variables of interest; REMOTE_ADDR and HTTP_X_FORWARDED_FOR. As many visitors access the internet via a third party (ie their ISP), REMOTE_ADDR does not always contain their IP address... it contains their ISP's address. If this is the case, most browsers then store the users IP address in the HTTP_X_FORWARDED_FOR variable. So, first, we check HTTP_X_FORWARDED_FOR, and then if that is empty, we try REMOTE_ADDR instead:

Dim sIPAddress

sIPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If sIPAddress="" Then sIPAddress = Request.ServerVariables("REMOTE_ADDR")

One other thing worth bearing in mind. If the user also accesses the internet via a Proxy server, then HTTP_X_FORWARDED_FOR will contain the Proxy's IP address. If this is the case, there is no way to get the users 'real' IP address.

Comments

  1. 03 Dec 2004 at 23:46

    what if when ever a user enters my site his/her ip address will be saved in a database? for me this is a good protection idea for no good users u save their ip in a database then if they do no good they will be denied the next time they enter the site... is there any code for this. thnx in  advance

  2. 15 Mar 2004 at 16:52

    How do you log a visitors IP address if register globals is off? Most hosts only have register globals off for security reasons.

  3. 16 Feb 2003 at 03:12

    how can i see the online users` ips? have you got an idea ?

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Get the visitor's IP address.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related discussion

Related podcasts

  • ASP.NET Caching and Performance

    Steve Smith, owner of ASP Alliance and Lake Quincy Media joins us today to teach us about some hidden gems in ASP.NET caching and performance. Steve’s expertise in this area comes from first-hand experience as Lake Quincy’s ad system serves over 60 requests per second and handles over 150 million...

Related jobs