Skip to Content

get_ip_address()

Safely retrieve the IP address.

PHP August 7, 2020

Usage

PHP
nebula()->get_ip_address($anonymize)

Parameters

$anonymize
(Optional) (Boolean) Return an anonymized IP address
Default: true

Parameter Notes

Note that the anonymize parameter defaults to true! If you are processing non-anonymized IP addresses you must first have consent from the user to comply with privacy regulations such as the GDPR.

Request or provide clarification »

Source File

Located in /libs/Utilities/Utilities.php on line 37.

No Hooks

This function does not have any filters or actions available. Request one?
PHP
        public function get_ip_address($anonymize=true){
            $ip_keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
            foreach ( $ip_keys as $key ){
                if ( array_key_exists($key, $_SERVER) === true ){
                    foreach ( explode(',', $_SERVER[$key]) as $ip ){
                        $ip = trim($ip);

                        if ( filter_var($ip, FILTER_VALIDATE_IP) ){ //Validate IP
                            return ( !empty($anonymize) )? wp_privacy_anonymize_ip($ip) : $ip; //Return the exact or anonymized IP address
                        }
                    }
                }
            }

            return false;
        }

Override

This function can not be short-circuited with an override filter. Request one?