Skip to content

Instantly share code, notes, and snippets.

@chrisblackwell
Created November 9, 2015 03:56
Show Gist options
  • Save chrisblackwell/5ff789cb010ce65c5e51 to your computer and use it in GitHub Desktop.
Save chrisblackwell/5ff789cb010ce65c5e51 to your computer and use it in GitHub Desktop.
Get the IP address of the current user
private function getUserIP()
{
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP)) {
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP)) {
$ip = $forward;
}
else {
$ip = $remote;
}
return $ip;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment