Back to Top

How to Get Real IP address of the Visitor in PHP

To get real IP address in PHP

It is very common that websites are retrieving the user’s IP address to implement access controls of users or visitors in PHP.The REMOTE_ADDR field in $_SERVER superglobal is normally held the user’s true IP address.But sometimes user uses proxy Server to hide own IP Address and try to access website by hiding behind proxies and at that time $_SERVER[‘REMOTE_ADDR’] returns the IP address of the proxy server not the user’s machine IP Address in PHP.

So. Here I am going to share function which will provide the real IP address of visitors machine even they are using a proxy server.

The HTTP_X_FORWARDED_FOR field contains the IP Address of the user if they are using Proxy Server.

Read about: Exception handling in PHP5

There is extra Server variable which might be available to determine the exact IP address of the client’s machine in PHP, they are HTTP_CLIENT_IP

Function to find real IP address in PHP


This function will first try to get the direct IP address of user machine.if fail to get then it will check for proxy server address using HTTP_X_FORWARDED_FOR and using some other headers.The HTTP_X_FORWARDED_FOR field is supported by most proxy servers to identify the IP address of the host which is requesting from the HTTP request through the proxy server and if everything gets fails, then finally get the IP address using REMOTE_ADDR.

I hope that you find above code useful! You can use this code anywhere you want. Did we miss anything? Tell me in the comment section :).

Share: Share on Facebook0Share on LinkedIn0Tweet about this on Twitter0Share on Google+0Share on Reddit0Buffer this pageDigg thisShare on StumbleUpon0Pin on Pinterest0

Comments (12)

  1. Very nice. The only issue I see is you missed a – on this line:

    if (($ipnum 184549375) && // Not in

    should be:

    if (($ipnum – 184549375) && // Not in

    1. Hi Jeff,

      Thanks it was code editor issue, updated code.

  2. I think there has been a copy paste issue, some missing characters in the code:-

    if (($ipnum 184549375) && // Not in
    ($ipnum – 1407188993) && // Not in
    ($ipnum – 1062666241)) // Not in

    1. Hi Mike,

      It was code editor issue.Updated and thanks :)

  3. Does this allow user ti fake his IP without any proxy server? Just simple add one more header like: HTTP_X_FORWARDED_FOR, then your code will get IP from there.

    1. HTTP_X_FORWARDED_FOR is already added.

  4. It’s really a great and useful piece of information.
    I am glad that you simply shared this useful info with us.
    Please keep us up to date like this. Thanks for sharing.

  5. Do you have any video of that? I’d want to find
    out some additional information.

  6. Having read this I thought it was really enlightening.
    I appreciate you spending some time and energy to put this short article
    together. I once again find myself personally spending way too much time both reading
    and commenting. But so what, it was still worthwhile!

  7. There is a problem when user running on IPv6.
    Does it work with IPv6?

    1. Blen, I’ve tested it and it is working fine on IPV6. Do you get any specific issue?

      1. Im sorry, it’s my mistake. I changed it for match ip which have both ipv6 and ipv4 and i forgot to close the function {.
        An other problem, could you help me: i dont know why, somtime, this function return an IP address like this: or some special character at the end of IP address.
        I have been using this as filter but sometime still have strange character in IP address:


        Do you have any solution for this?

Leave a Reply

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

Most Popular Posts

GoTo statement in PHP

Posted on 3 years ago


Table layout using DIV tags

Posted on 2 years ago


Mysql String Function FIND_IN_SET( )

Posted on 3 years ago


Post via e-mail in WordPress

Posted on 3 years ago