PHP Basics

Use PHP To Get The Real IP Address


If you want to record the visitors IP Address there are a few things you need to remember, computers on shared internet connections can have different IP Addresses and visitors coming to your website through a proxy will have different IP addresses.

Computers coming through a proxy, the IP address will come through as the proxy IP address.

Below is a PHP snippet which you can use which will get the real IP Address of the visitor.

function getRealIpAddr()
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    return $ip;
Back to top

Learn how to code with Treehouse

  • Learn projects with access to 1000+ videos
  • Practice live with our Code Challenge Engine
  • Get help in our members-only forums

Start with a 7 day free trial