Created
May 9, 2021 16:35
-
-
Save alewolf/f657b4c77d356e86c7244c2961a8b67a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// https://stackoverflow.com/a/2031935/4688612 | |
// https://stackoverflow.com/q/67277544/4688612 | |
protected function get_visitor_ip(): string | |
{ | |
$proxy_headers = [ | |
'HTTP_CF_CONNECTING_IP', // Cloudflare | |
'HTTP_TRUE_CLIENT_IP', // Cloudflare Enterprise | |
'HTTP_INCAP_CLIENT_IP', // Incapsula | |
'HTTP_X_SUCURI_CLIENTIP', // Sucuri | |
'HTTP_FASTLY_CLIENT_IP', // Fastly | |
'HTTP_X_FORWARDED_FOR', // any proxy | |
'HTTP_X_FORWARDED', | |
'HTTP_X_CLUSTER_CLIENT_IP', | |
'HTTP_FORWARDED_FOR', | |
'HTTP_FORWARDED', | |
'HTTP_CLIENT_IP', | |
'REMOTE_ADDR' | |
]; | |
foreach ($proxy_headers as $key) { | |
if (array_key_exists($key, $_SERVER) === true) { | |
foreach (explode(',', $_SERVER[$key]) as $ip) { | |
$ip = trim($ip); // just to be safe | |
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) { | |
return $ip; | |
} | |
} | |
} | |
} | |
return ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment