The best method to find client’s IP Address in PHP.


Read {count} times since 2020

This simple function will help you to find the client’s IP. You could use $_SERVER[‘REMOTE_ADDR’] , but this method will not returns the actual IP address.

To get the real IP address use this method.

function getUserIpAddr(){
    if (!empty($_SERVER[‘HTTP_CLIENT_IP’])){
        return $_SERVER[‘HTTP_CLIENT_IP’];
    }else if (!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’])){
        $ite=explode(‘,’,$_SERVER[‘HTTP_X_FORWARDED_FOR’]); return str_replace(" ",",$ite[0]);
        return $_SERVER[‘REMOTE_ADDR’];
} $cip=getUserIpAddr();

The above function will returns the IP address of the client. You could use the variable $cip to get the IP.
Example (printing client’s IP) :

echo "Your IP Address is ".$cip;

Show Comments