Find a visitor’s IP Address in ASP

Do you want to know the IP address of a visitor? This can be useful for many reasons, such as tracking site usage or blocking access to specific people. Here's how you can find it.

In an ASP page, within the ASP tags, <% ... %>, you can retrieve the IP address of a user through the ServerVariables collection of the Request object. To do this, you use the code Request.ServerVariables("REMOTE_ADDR").

Display The IP Address

So, if you want to display the IP Address to the user then the following page will suffice:

ASP:
  1. <%@language="VBScript"%>
  2. <%
  3. Response.Write "Hello! Your IP Address is: " & Request.ServerVariables("REMOTE_ADDR")
  4. %>

Exploring Request.ServerVariables

You might be wondering what other information you can get from Request.ServerVariables, well you can find out with the following script which displays all the variables in it along with their values if set, in a HTML table:

ASP:
  1. <%@language="VBScript"%>
  2. <table border="1">
  3. <tr><th>Variable</th><th>Value</th></tr>
  4. <%
  5. Dim variable
  6. For Each variable In Request.ServerVariables
  7.    Response.Write "<tr><td>" & variable & "</td>"
  8.    Response.Write "<td>" & Request.ServerVariables(variable) & "</td></tr>"
  9. Next
  10. %>
  11. </table>

Improving the find IP code

Finally, the following code works harder to find the true IP of the user by checking to see if the visitor is using a proxy:

ASP:
  1. Function UserIP()
  2.  ’ This returns the True IP of the client calling the requested page
  3.  ’ Checks to see if HTTP_X_FORWARDED_FOR has a value then the client is operating via a proxy
  4.  UserIP = Request.ServerVariables ( "HTTP_X_FORWARDED_FOR" )
  5.  If UserIP = "" Then
  6.      UserIP = Request.ServerVariables ( "REMOTE_ADDR" )
  7.  End if
  8. End Function

If you have a website, consider joining the Money4Banners advertising network. They will pay you £10 + £5 each month for displaying a small advert on three of your pages, regardless of traffic. American webmasters are welcome, and since £10 == $20, you make more!


8 Responses to “Find a visitor’s IP Address in ASP”  

  1. Gravatar Icon 1 bob

    Helped a LOT! Thanks very much!

  2. Gravatar Icon 2 Runner90

    How Can I transfer this IP Address to a Log File in My HostMachine….

  3. Gravatar Icon 3 radu

    Hi there,

    I have a problem with finding out the real IP of the visitor. Looks that the simple code:
    Request.ServerVariables(”REMOTE_ADDR”)
    is not working and I always get the IP of the server instead.

    Do you have any idea why?

    Thanks,
    Radu

  4. Gravatar Icon 4 Jensen

    Dear Radu and All,

    I do have same problem with you which Request.ServerVariables(”REMOTE_ADDR”)
    is always return me the server IP itself.

    May i know that do you found out the solution alreday?

    Mind to share what is the solution?

    Thanks,
    Jensen

  5. Gravatar Icon 5 farid

    i was wounderin gif anybody can tell me. i have chat system i want to place this code in guest table . if i want to block their ip i can do . is there anyone can help me how to store visitiros ip address in data base then i be able to block or give them access to use certain page?
    help will be appreciated

  6. Gravatar Icon 6 sam

    thnx alot

  7. Gravatar Icon 7 hamid

    hi
    i want source code to find visitor ip address other website
    example :
    get visitor ip www.discomoose.org

  8. Gravatar Icon 8 Rajeev Kumar

    I want to source code in vb.net or c# to find out the IP Address of other website visitor.
    Thanks
    Rajeev

Leave a Reply

You must log in to post a comment.


Categories