WC_Geolocation::get_ip_addresspublic staticWC 1.0

Get current user IP Address.

Method of the class: WC_Geolocation{}

No Hooks.

Returns

String.

Usage

$result = WC_Geolocation::get_ip_address();

WC_Geolocation::get_ip_address() code WC 9.9.4

public static function get_ip_address() {
	if ( isset( $_SERVER['HTTP_X_REAL_IP'] ) ) {
		return sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REAL_IP'] ) );
	} elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
		// Proxy servers can send through this header like this: X-Forwarded-For: client1, proxy1, proxy2
		// Make sure we always only send through the first IP in the list which should always be the client IP.
		$value = trim( current( preg_split( '/,/', sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) ) ) );
		// Account for the '<IPv4 address>:<port>', '[<IPv6>]' and '[<IPv6>]:<port>' cases, removing the port.
		// The regular expression is oversimplified on purpose, later 'rest_is_ip_address' will do the actual IP address validation.
		$value = preg_replace( '/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\:.*|\[([^]]+)\].*/', '$1$2', $value );
		return (string) rest_is_ip_address( $value );
	} elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
		// Make sure we always only send through the first IP in the list which should always be the client IP.
		$value = trim( current( preg_split( '/,/', sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) ) ) );
		return (string) rest_is_ip_address( $value );
	}
	return '';
}