Thursday 21 July 2016

PHP Function to get IP address of a client

Easy and simple function to get ip address of client address. Below function gets the clients system ip address in normal cases and if the client is present in a proxy then it will get the real ip address.

function getIPAddress() {
    if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}


Use the above php code snippet to show the ip address of the client in your webpage.

No comments:

Post a Comment