WP_Community_Events::get_unsafe_client_ip(): string|false
- Since
- 4.8.0
- Source
wp-admin/includes/class-wp-community-events.php:241
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
- Scaling
- Scales with input
- Instructions
- 5–27
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 29.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5–6 | none |
| always | 12–15 | wp_privacy_anonymize_ip() |
$header | 18 | explode() |
$header | 25–27 | explode(), wp_privacy_anonymize_ip() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 29 | 5–27 | 6 | |
| 8.5 | 29 | 5–27 | 6 | |
| 8.4 | 29 | 5–27 | 6 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 32 | 5–30 | 6 | |
| 8.2 | 32 | 5–30 | 6 | |
| 8.1 | 32 | 5–30 | 6 | |
| 7.4 | 32 | 5–30 | 6 |
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
- wp_privacy_anonymize_ip()Returns an anonymized IPv4 or IPv6 address.
Used by · 2
- WP_Community_Events::get_request_args()Builds an array of args to use in an HTTP request to the w.org Events API.
- wp_localize_community_events()Localizes community events data that needs to be passed to dashboard.js.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.