wppaste
WordPress

WP_Community_Events::get_unsafe_client_ip(): string|false

Since
4.8.0
Source
wp-admin/includes/class-wp-community-events.php:241
Determines the user's actual IP address and attempts to partially anonymize an IP address by converting it to a network ID.

Description

Geolocating the network ID usually returns a similar location as the actual IP, but provides some privacy for the user.

$_SERVER['REMOTE_ADDR'] cannot be used in all cases, such as when the user is making their request through a proxy, or when the web server is behind a proxy. In those cases, $_SERVER['REMOTE_ADDR'] is set to the proxy address rather than the user's actual address.

Modified from https://stackoverflow.com/a/2031935/450127, MIT license.
Modified from https://github.com/geertw/php-ip-anonymizer, MIT license.

SECURITY WARNING: This function is NOT intended to be used in circumstances where the authenticity of the IP address matters. This does NOT guarantee that the returned address is valid or accurate, and it can be easily spoofed.

Compatibility

WordPress
since 4.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

string|false
The anonymized address on success; the given address or false on failure.

Performance profile

How much work a call to WP_Community_Events::get_unsafe_client_ip() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
5–27

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 29.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Community_Events::get_unsafe_client_ip() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always5–6none
always12–15wp_privacy_anonymize_ip()
$header18explode()
$header25–27explode(), wp_privacy_anonymize_ip()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev295–276
8.5295–276
8.4295–2763 fewer instructions than PHP 8.3
8.3325–306
8.2325–306
8.1325–306
7.4325–306

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 1

Used by · 2

Source code

	public static function get_unsafe_client_ip() {		$client_ip = false; 		// In order of preference, with the best ones for this purpose first.		$address_headers = array(			'HTTP_CLIENT_IP',			'HTTP_X_FORWARDED_FOR',			'HTTP_X_FORWARDED',			'HTTP_X_CLUSTER_CLIENT_IP',			'HTTP_FORWARDED_FOR',			'HTTP_FORWARDED',			'REMOTE_ADDR',		); 		foreach ( $address_headers as $header ) {			if ( array_key_exists( $header, $_SERVER ) ) {				/*				 * HTTP_X_FORWARDED_FOR can contain a chain of comma-separated				 * addresses. The first one is the original client. It can't be				 * trusted for authenticity, but we don't need to for this purpose.				 */				$address_chain = explode( ',', $_SERVER[ $header ] );				$client_ip     = trim( $address_chain[0] ); 				break;			}		} 		if ( ! $client_ip ) {			return false;		} 		$anon_ip = wp_privacy_anonymize_ip( $client_ip, true ); 		if ( '0.0.0.0' === $anon_ip || '::' === $anon_ip ) {			return false;		} 		return $anon_ip;	}

Changelog

Introduced in 4.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-admin/includes/class-wp-community-events.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.