Last active
September 28, 2018 15:43
-
-
Save wpscholar/881c76f5e0588f4d2d979fb21f0a66d0 to your computer and use it in GitHub Desktop.
What are the security implications of overwriting `REMOTE_ADDR`? Do we need to explicitly check for trusted IP addresses (e.g. https://www.cloudflare.com/ips/)? Should 'HTTP_X_FORWARDED_FOR' ever be used? See https://kb.sucuri.net/firewall/Troubleshooting/same-user-ip
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 client IP address. | |
* | |
* @return string | |
*/ | |
function mm_get_client_ip() { | |
// Default to REMOTE_ADDR | |
$ip = $_SERVER['REMOTE_ADDR']; | |
$proxy_headers = array( | |
'HTTP_CF_CONNECTING_IP', // CloudFlare | |
'HTTP_INCAP_CLIENT_IP', // Incapsula | |
'HTTP_X_SUCURI_CLIENTIP', // Sucuri | |
'HTTP_X_FORWARDED_FOR', // Any Proxy | |
); | |
// Check for alternate headers indicating a forwarded IP address | |
foreach ( $proxy_headers as $proxy_header ) { | |
if ( isset( $_SERVER[ $proxy_header ] ) ) { | |
$forwarded_ips = explode( ',', $_SERVER[ $proxy_header ] ); | |
$forwarded_ip = array_shift( $forwarded_ips ); | |
if ( $forwarded_ip ) { | |
$ip = $forwarded_ip; | |
break; | |
} | |
} | |
} | |
return $ip; | |
} | |
$_SERVER['REMOTE_ADDR'] = mm_get_client_ip(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment