Been having invalid IPV6 addresses on and off for a little while, so this seems to resolve that and updates the IPs correctly, and some filtering on the IPV4 compatible prefix. Takes care of proxies as the original, and lets you convert the IPV4 address to a compatible IPV6 address.
function getip($convertToIPv6 = false)
{
$trustedHeaders = [
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'HTTP_CF_CONNECTING_IP',
'REMOTE_ADDR'
];
$ip = '';
foreach ($trustedHeaders as $header) {
if (!empty($_SERVER[$header])) {
$ip = $_SERVER[$header];
break;
}
}
if (empty($ip)) {
$ip = $_SERVER['REMOTE_ADDR'];
}
if (strpos($ip, ',') !== false) {
$ip = explode(',', $ip)[0];
}
if (strpos($ip, '::ffff:') === 0) {
$ip = substr($ip, 7);
}
$ip = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
if ($convertToIPv6 && filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$ip = '::ffff:' . $ip;
}
return $ip;
}
Last edited by STorrents2020 on 2023-05-21 18:59:39