Fetch IP Address

Echo89 1 Tallied Votes 604 Views Share

Just a quicky!

function fetch_ip()
{
	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;
}
pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

If this does not provide you with what you need, have a look at this discussion on SO.

solutionsinfini 0 Newbie Poster

Hello Caelan Stewart,

Thanks my friend, it's a good one. If u have that kind of stuff more,please share with us.

Regards,
Solutions Infini.

coreyavis 13 Dev Poster

Maybe you might find this one better:

function getip() {
$ip = "";
if (isset($_SERVER)) {
    if (!empty($_SERVER['HTTP_CLIENT_IP']) && validip($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) && validip($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
        $ip = $_SERVER['REMOTE_ADDR'];
    }
}else{
    if (getenv('HTTP_CLIENT_IP') && validip(getenv('HTTP_CLIENT_IP'))) {
        $ip = getenv('HTTP_CLIENT_IP');
    }else if (getenv('HTTP_X_FORWARDED_FOR') && validip(getenv('HTTP_X_FORWARDED_FOR'))) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    }else{
        $ip = getenv('REMOTE_ADDR');
    }
}
return $ip;
}

function validip($ip) {
if (!empty($ip) && $ip == long2ip(ip2long($ip))) {
    $reserved_ips = array(
        array('0.0.0.0', '0.255.255.255'),
        array('10.0.0.0', '10.255.255.255'),
        array('100.64.0.0', '100.127.255.255'),
        array('127.0.0.0', '127.255.255.255'),
        array('169.254.0.0', '169.254.255.255'),
        array('172.16.0.0', '172.31.255.255'),
        array('192.0.2.0', '192.0.2.255'),
        array('192.88.99.0', '192.88.99.255'),
        array('192.168.0.0', '192.168.255.255'),
        array('198.18.0.0', '198.19.255.255'),
        array('198.51.100.0', '198.51.100.255'),
        array('203.0.113.0', '203.0.113.255'),
        array('255.255.255.0', '255.255.255.255')
    );
    foreach ($reserved_ips as $r) {
        $min = ip2long($r[0]);
        $max = ip2long($r[1]);
        if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max)) return false;
    }
    return true;
}else{
    return false;
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.