Skip to content

Instantly share code, notes, and snippets.

@serverok
Created September 8, 2024 06:59
Show Gist options
  • Save serverok/a6c084d6b6a11a674c93dd2b89bf46bf to your computer and use it in GitHub Desktop.
Save serverok/a6c084d6b6a11a674c93dd2b89bf46bf to your computer and use it in GitHub Desktop.
PHP function to find visitor IP
function find_user_ip()
{
foreach (array('HTTP_CF_CONNECTING_IP', 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
if (array_key_exists($key, $_SERVER) && !empty($_SERVER[$key])) {
foreach (@explode(',', ''.$_SERVER[$key]) as $ip) {
$ip = trim($ip);
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
return $ip;
}
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment