Usage: var_dump(get_client_ip());
/**
* Detect is localhost
*
* @return boolean
*/
function isLocalHost()
{
$whitelist = [
'127.0.0.1',
'::1',
];
return in_array($_SERVER['REMOTE_ADDR'], $whitelist);
}
/**
* Get client ip, when getenv supported (maybe cli)
*
* @return string
*/
function get_client_ip()
{
$ipaddress = '';
if (isLocalHost()) {
$ipaddress = getLocalIp();
} else {
if (getenv('HTTP_CLIENT_IP')) {
$ipaddress = getenv('HTTP_CLIENT_IP');
} elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('HTTP_X_FORWARDED')) {
$ipaddress = getenv('HTTP_X_FORWARDED');
} elseif (getenv('HTTP_FORWARDED_FOR')) {
$ipaddress = getenv('HTTP_FORWARDED_FOR');
} elseif (getenv('HTTP_FORWARDED')) {
$ipaddress = getenv('HTTP_FORWARDED');
} elseif (getenv('REMOTE_ADDR')) {
$ipaddress = $ipaddress = getenv('REMOTE_ADDR');
} else {
/**
* Return to method 2
*/
$ipaddress = get_client_ip2();
}
}
return $ipaddress;
}
/**
* Get client ip, when running on webserver
*
* @return void
*/
function get_client_ip2()
{
$ipaddress = '';
if (isLocalHost()) {
$ipaddress = getLocalIp();
} else {
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
} elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_FORWARDED'])) {
$ipaddress = $_SERVER['HTTP_FORWARDED'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ipaddress = $_SERVER['REMOTE_ADDR'];
} else {
$ipaddress = 'UNKNOWN';
}
}
return $ipaddress;
}
function getLocalIp()
{
if (defined('PHP_MAJOR_VERSION') && PHP_MAJOR_VERSION >= 5) {
$localIP = gethostbyname(gethostname());
} else {
$localIP = gethostbyname(php_uname('n'));
}
return $localIP;
}
Comments
Post a Comment
Bila Ada posting yang kurang, atau error atau yang lainnya, silahkan tinggalkan komentar agar artikel/post di perbaiki.
Jangan Lupa Cek Box "Notify Me" agar tahu komentar kamu dibalas oleh saya.
If there are any posts that are missing, or error or anything else, please leave a comment for the article / post to be fixed.
Do not Forget Check Box "Notify Me" to know our comments replied by me.