ASP Code, ASP.Net Code, PHP Code

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

25 Comments

  1. 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. 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. 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. Sorry, I've never had to do that, so I have no idea. You may want to look on Daniweb or somewhere similar

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

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

  8. thanks for the info.. it helped
    garaki

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

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

  11. Nigeria IP's not listed, why?

  12. Nigeria IP's is not listed.why?

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

  14. 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?

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

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

  17. Good work, thank you!

  18. Hi every one my name is AlAa

    My problem with $_SERVER['remote_addr']

    I'm not sure if I'm having a problem with the installation of PHP on the server or if there's something that I'm not doing right in the code, but I am trying to capture the IP address of anyone that is trying to log into a secure part of a site. Regardless of my IP address, when I do:

    Code:

    echo $_SERVER['remote_addr'];

    It returns '192.168.1.1'. This is the first time I've written code that is being used on a Windows Server, so I don't know if that has anything to do with it.

    I've run phpinfo(), and in the PHP Variables section, it shows "_SERVER["REMOTE_ADDR"]" : 192.168.1.1. Does this mean that the php.ini file is setup wrong, or am I missing something?

    i am running my own server on windows 7 iis7.5 with php as fastcgi and i am behind a router with a dynamic ip not static and i am using
    http://www.no-ip.com/ that give me a link to my router and i am opening 80 port in my router forwarding it to my id address 192.168.1.5 and i have tested a php file that contains

    tested it using tor network that give me ip out of my network to test that file always give me the same result 192.168.1.1 i am trying to get IP address of the visitor and i asked someone of my friends to use this form it sends the same ip

    i hope someone tells me what i am doing wrong and the solution for this problem

    Thanks in advance

  19. FeedsAlive lets you know your visitors informations from your sites. You can view how then they visited on each pages before moving to other page. We are providing this features at free of costs.

  20. This sounds great but I am struggling to locate this code within my PHP website.

    I have changed the format of my website to PHP for this very purpose. I.e. I have changed the prefix of all my pages from .HTML to .PHP and fixed links accordingly.

    Should I put this code in the head, body, before the head etc?

    I hope someone can help!

    Yours struggling, Michael.

  21. …perhaps I should have said…

    I am trying to get anlaytics to output visitor ip addresses rather than the proxy server ip of my website visitors.

    Thanks.

  22. Tally academy in Delih

    I have started using this code and is working fine with the website, indeed a good tool for webmasters

  23. I used the following code and it is working fine.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
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.
Anti-spam image