Last active
June 8, 2017 01:58
-
-
Save gaoxt/867cd48ddd57ab3a60de63546857a55c to your computer and use it in GitHub Desktop.
getip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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