November 28th, 2006

How to retrieve a visitor's IP address

Every visitor to your site or web application has an IP address. It is quite handy to be able to get that address. It can be used for security logging, or perhaps tracing. It can also be used to determine where they are in the world, or at least where their ISP is.

The difficulty is when they're behind a proxy of some sort, you only see the IP address of the proxy server. So, here are the code snippets in PHP, ASP and .Net that first check for an IP addresses that's forwarded from behind a proxy, and if there's none then just get the IP address.

Here it is in PHP


<?
    if (getenv(HTTP_X_FORWARDED_FOR)) {
        $ip_address = getenv(HTTP_X_FORWARDED_FOR);
    } else {
        $ip_address = getenv(REMOTE_ADDR);
    }
?>

And here's how to get the IP address in ASP


<%
    ip_address = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    if ip_address = "" then
        ip_address = Request.ServerVariables("REMOTE_ADDR")
    end if
%>

And here's the IP retriever with proxy detection in .Net (C#)


public string IpAddress()
{
    string strIpAddress;
    strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (strIpAddress == null)
    {
       strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
    }
    return strIpAddress;
}

And here's the same IP retriever with proxy detection in .Net, but in VB.Net


Public Function IpAddress()
    Dim strIpAddress As String
    strIpAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    If strIpAddress = "" Then
       strIpAddress = Request.ServerVariables("REMOTE_ADDR")
    End If
    IpAddress = strIpAddress
End Function
Share

19 Responses to ' How to retrieve a visitor's IP address '

Subscribe to comments with RSS or TrackBack to ' How to retrieve a visitor's IP address '.

  1. Darius said,

    on December 21st, 2006 at 2:10 am

    You do not need to search for working free web proxies. All is here.
    Find free web proxy for desired country!
    All proxies are regulary checked if they still alive.

    enjoy it!

  2. aarvee said,

    on February 6th, 2007 at 4:17 am

    I used the IP retriever using C#.But it is always returning the IP Address of my server instead of visitor's ip address.Is there any alternative

  3. Adrian George said,

    on February 10th, 2007 at 2:53 pm

    Hi, I would like to find out, if there is a possibility to retrieve an IP knowing the yahoo messenger IP. I am asking this, because I sometimes get uninvited people, who use a pornografic ID, and their messages are as well their ID… I just wanna know if there is any way to get their IP so I may trace them… Doesn't matter if they are using a Net Caffee or somthing similar. Thank you…

  4. Justin Cook said,

    on February 11th, 2007 at 10:55 am

    Sorry, I've never had to do that, so I have no idea. You may want to look on Daniweb or somewhere similar

  5. kb said,

    on May 31st, 2007 at 7:16 pm

    how do u get there in the first place and it would be nice if your site had a whole list of proxy sites since im always looking for new ones since my skool always find thems after 1 day

  6. Essay Town said,

    on June 6th, 2007 at 7:02 pm

    good info

    http://www.essaytown.com

  7. Mizan said,

    on July 3rd, 2007 at 5:58 am

    I want to get IP address of visitor to start an online voting which able to know the visitor cast vote single time only.

    Thanks

  8. Justin Cook said,

    on July 3rd, 2007 at 7:39 am

    For that I would just look for an open source polling class. That functionality is always built in

  9. kaka said,

    on June 27th, 2008 at 11:15 am

    thanks for the info.. it helped
    garaki

  10. Ashish said,

    on June 15th, 2009 at 12:41 am

    My website is Developed in ASP, How we findout Visite IP Address?

  11. Dee said,

    on June 22nd, 2009 at 6:28 pm

    I am writing a Tcp ChatServer and ChatClient application in windows using c#. Now I want the client to be able to get the address of the server dynamically.


  12. on June 17th, 2010 at 3:51 pm

    Nigeria IP's not listed, why?


  13. on June 17th, 2010 at 4:08 pm

    Nigeria IP's is not listed.why?


  14. on July 31st, 2010 at 10:50 pm

    nice info…….. :)

    http://itweet.cz.cc
    http://free-affiliatemarketing.cz.cc
    http://likeitnow.cz.cc

  15. shivam said,

    on October 26th, 2010 at 2:28 pm

    nice info…..
    but we are creating a project in j2ee so we need a code for ip tracing
    pls help me sir……
    pls reply soon

  16. Pete said,

    on November 30th, 2010 at 7:23 am

    I am trying to determine whi is posting stupid messages via my contact form. Where would the IP address be listed and could I attach it somehow to the comments?

  17. Prashant said,

    on December 12th, 2010 at 3:31 am

    Sir,

    I'm trying to track the visitors in my web page can you provide me with code that scan the IP address of visitors and mail me and store that data online. Now I'm using www3.clustrmaps.com to track the visitors but it's limited and give geographical view only i want to track each and every web visit to my page.

    Thanks & Regards,

    Prashant Soni

  18. HomeList said,

    on March 21st, 2011 at 3:59 pm

    Thanks for this quick guide. I'm using the php IP retriever now!

  19. mani said,

    on July 4th, 2011 at 4:54 pm

    Good work, thank you!

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word