Snippet get real visitor/user ip (support localhost XAMPP)
function isLocalHost()
{
$whitelist = [
'127.0.0.1',
'::1',
];
return in_array($_SERVER['REMOTE_ADDR'], $whitelist);
}
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 {
$ipaddress = get_client_ip2();
}
}
return $ipaddress;
}
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;
}
Usage
<?php
var_dump(get_client_ip());
Created at 2020-10-16 21:26:00
Updated at 2024-02-20 05:38:36
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.