Skip to Content
Menu

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 »

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

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

    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, $this->super->server) === true ){
                        foreach ( explode(',', $this->super->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?