Skip to content

Instantly share code, notes, and snippets.

@alanef
Created March 30, 2021 18:45
Show Gist options
  • Select an option

  • Save alanef/963840a90fed3454a7434c956931b13d to your computer and use it in GitHub Desktop.

Select an option

Save alanef/963840a90fed3454a7434c956931b13d to your computer and use it in GitHub Desktop.
function get_ip( $ipaddress = null ) {
if ( ! empty( $ipaddress ) ) {
if ( false === WP_Http::is_ip_address( $ipaddress ) ) {
return '0.0.0.0';
}
return $ipaddress;
}
$ipaddress = '0.0.0.0';
if ( getenv( 'HTTP_CF_CONNECTING_IP' ) ) {
$ipaddress = getenv( 'HTTP_CLIENT_IP' );
} elseif ( getenv( 'HTTP_CLIENT_IP' ) ) {
$ipaddress = getenv( 'HTTP_CLIENT_IP' );
} elseif ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
$ipaddress = getenv( 'HTTP_X_FORWARDED_FOR' );
} elseif ( getenv( 'HTTP_X_FORWARDED' ) ) {
$ipaddress = getenv( 'HTTP_X_FORWARDED' );
} elseif ( getenv( 'HTTP_FORWARDED_FOR' ) ) {
$ipaddress = getenv( 'HTTP_FORWARDED_FOR' );
} elseif ( getenv( 'HTTP_FORWARDED' ) ) {
$ipaddress = getenv( 'HTTP_FORWARDED' );
} elseif ( getenv( 'REMOTE_ADDR' ) ) {
$ipaddress = getenv( 'REMOTE_ADDR' );
}
// sanitize IP address
if ( false === WP_Http::is_ip_address( $ipaddress ) ) {
$ipaddress = '0.0.0.0';
}
return $ipaddress;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment