How to Get Real Client IP Address In PHP

If you are a web developer you might need to get the real IP address of the user who is using your application. IP addresses are needed to run various validation tests such as spam prevention. In PHP there are few ways to find the IP address of a client. However in some scenarios you may not get the real IP addresses of users who are behind a proxy server. But there are few things to try in order to accomplish our goal. In this article we have discussed about How to Get Real Client IP Address In PHP.

Get IP address using environment variables in PHP

In PHP environment variables are variables in PHP global namespace that provided from the environment which the PHP parser is running. Whenever the script is called the system environment posts an array with predefined variables. This array includes few variables that can be used to extract the IP address of the client who called the script. The method we are using here is getenv().

 function getClientIP_env() {
 if (getenv('HTTP_CLIENT_IP'))
 $ip  = getenv('HTTP_CLIENT_IP');
 else if(getenv('HTTP_X_FORWARDED_FOR'))
 $ip  = getenv('HTTP_X_FORWARDED_FOR');
 else if(getenv('HTTP_X_FORWARDED'))
 $ip  = getenv('HTTP_X_FORWARDED');
 else if(getenv('HTTP_FORWARDED_FOR'))
 $ip  = getenv('HTTP_FORWARDED_FOR');
 else if(getenv('HTTP_FORWARDED'))
 $ip  = getenv('HTTP_FORWARDED');
 else if(getenv('REMOTE_ADDR'))
 $ip  = getenv('REMOTE_ADDR');
 else
 $ip  = 'UNKNOWN';
return $ip;
 }

How to Get Real Client IP Address In PHP

Get IP address using server variables in PHP

You can use an array called $_SERVER to Get Real Client IP Address In PHP. This is the most popular way of extracting client IP addresses. A web server creates an array with path information, header information and script location information. This array, $_SERVER can be called in anywhere of your PHP script. Here we are using few variables in  $_SERVER array to identify the real IP address of a client.

In this function we are using the same variables as we did using getenv() method. The only difference is the location we pull these data.

function getClientIP_svr() {
if ($_SERVER['HTTP_CLIENT_IP'])
$ip  = $_SERVER['HTTP_CLIENT_IP'];
else if($_SERVER['HTTP_X_FORWARDED_FOR'])
$ip  = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if($_SERVER['HTTP_X_FORWARDED'])
$ip  = $_SERVER['HTTP_X_FORWARDED'];
else if($_SERVER['HTTP_FORWARDED_FOR'])
$ip  = $_SERVER['HTTP_FORWARDED_FOR'];
else if($_SERVER['HTTP_FORWARDED'])
$ip  = $_SERVER['HTTP_FORWARDED'];
else if($_SERVER['REMOTE_ADDR'])
$ip  = $_SERVER['REMOTE_ADDR'];
else
$ip  = 'UNKNOWN';

return $ip;
}

Get the IP address of a client behind a proxy

HTTP_X_FORWARDED_FOR will give you the internal or LAN IP address of the client. If the client has been forwarded from multiple proxies this would return an comma separated array with particular IP addresses. You may need to split up the list if you need Get Real Client IP Address In PHP. HTTP_X_FORWARDED_HOST and HTTP_X_FORWARDED_SERVER will return same values that SERVER_NAME reruns in a non-proxy environment.

Lakshan Jayasinghe

Lakshan is a passionate blogger and a geek! ! He is graduated from University of Colombo, School of Computing. He is currently working as a Software Engineer in Colombo, Sri Lanka with a passion for technology, web application developing, design and every kind of gadgetry. Contact : Email / Facebook / Google+ / Twitter / LinkedIn

You may also like...

2 Responses

  1. Sajjad says:

    I think this function also help to find real IP address.
    function getrealip()
    {
    if (isset($_SERVER)){
    if(isset($_SERVER[“HTTP_X_FORWARDED_FOR”])){
    $ip = $_SERVER[“HTTP_X_FORWARDED_FOR”];
    if(strpos($ip,”,”)){
    $exp_ip = explode(“,”,$ip);
    $ip = $exp_ip[0];
    }
    }else if(isset($_SERVER[“HTTP_CLIENT_IP”])){
    $ip = $_SERVER[“HTTP_CLIENT_IP”];
    }else{
    $ip = $_SERVER[“REMOTE_ADDR”];
    }
    }else{
    if(getenv(‘HTTP_X_FORWARDED_FOR’)){
    $ip = getenv(‘HTTP_X_FORWARDED_FOR’);
    if(strpos($ip,”,”)){
    $exp_ip=explode(“,”,$ip);
    $ip = $exp_ip[0];
    }
    }else if(getenv(‘HTTP_CLIENT_IP’)){
    $ip = getenv(‘HTTP_CLIENT_IP’);
    }else {
    $ip = getenv(‘REMOTE_ADDR’);
    }
    }
    return $ip;
    }

Leave a Reply

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