Skip to content

Instantly share code, notes, and snippets.

@gaoxt
Last active June 8, 2017 01:58
Show Gist options
  • Save gaoxt/867cd48ddd57ab3a60de63546857a55c to your computer and use it in GitHub Desktop.
Save gaoxt/867cd48ddd57ab3a60de63546857a55c to your computer and use it in GitHub Desktop.
getip
<?php
function get_client_ip()
{
$ip_source = array(
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'REMOTE_ADDR'
);
foreach ($ip_source as $key) {
if (array_key_exists($key, $_SERVER)) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
$ip = trim($ip);
if ((bool) filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_IPV4 |FILTER_FLAG_NO_PRIV_RANGE |FILTER_FLAG_NO_RES_RANGE
)) {
return $ip;
}
}
}
}
return 'unknow';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment