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. 01 Jan 1999 at 00:00

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

  2. 16 Feb 2003 at 03:12

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

  3. 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.

  4. 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

  5. 29 Mar 2010 at 10:58

    this online user's ip tracking is amazing use and have fun and also find people who harm your computer/ [url=http://dvdcopysoftwareguide.com/]dvd copy software[/url]

Leave a comment

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

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Related discussion

Related podcasts

  • Herding Code: Herding Code 59: Web Standards with Milan Negovan

    Published 10 months ago, running time 0h56m

    In this episode of the Herding Code Podcast, the guys sit down with Milan Negovan of ASP.NET Resources to discuss web standards, usability and accessibility.  Milan also shares his opinions on the onslaught of new technologies coming out of Redmond, why developers should avoid big conferences, th... web standards, asp, out, usability

Events coming up

Want to stay in touch with what's going on? Follow us on twitter or Facebook!