Created
July 25, 2018 13:49
-
-
Save gelinger777/a49596955fde1230c804b3df9b179500 to your computer and use it in GitHub Desktop.
sudo nano /override/classes/Tools
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
/** | |
* Get the server variable REMOTE_ADDR, or the first ip of HTTP_X_FORWARDED_FOR (when using proxy) | |
* | |
* @return string $remote_addr ip of client | |
*/ | |
public static function getRemoteAddr() | |
{ | |
if (function_exists('apache_request_headers')) { | |
$headers = apache_request_headers(); | |
} else { | |
$headers = $_SERVER; | |
} | |
// CloudFlare support | |
if (array_key_exists('HTTP_CF_CONNECTING_IP', $headers)) { | |
// Validate IP address (IPv4/IPv6) | |
if (filter_var($headers['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP)) { | |
return $headers['HTTP_CF_CONNECTING_IP']; | |
} | |
} | |
if (array_key_exists('X-Forwarded-For', $headers)) { | |
$_SERVER['HTTP_X_FORWARDED_FOR'] = $headers['X-Forwarded-For']; | |
} | |
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && (!isset($_SERVER['REMOTE_ADDR']) | |
|| preg_match('/^127\..*/i', trim($_SERVER['REMOTE_ADDR'])) || preg_match('/^172\.16.*/i', trim($_SERVER['REMOTE_ADDR'])) | |
|| preg_match('/^192\.168\.*/i', trim($_SERVER['REMOTE_ADDR'])) || preg_match('/^10\..*/i', trim($_SERVER['REMOTE_ADDR'])))) { | |
if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',')) { | |
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); | |
return $ips[0]; | |
} else { | |
return $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} | |
} else { | |
return $_SERVER['REMOTE_ADDR']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment