src/Util/Net.php
public static function getIP() {

        //Just get the headers if we can or else use the SERVER global
        if ( function_exists( 'apache_request_headers' ) ) {
            $headers = apache_request_headers();
        } else {
            $headers = $_SERVER;
        }

        // $filter_option = FILTER_FLAG_IPV4;
        // $filter_option = FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
        $filter_option = 0;

        $the_ip = false;

        // Check Cloudflare headers
        if ( $the_ip === false && array_key_exists( 'HTTP_CF_CONNECTING_IP', $headers ) ) {
            $pieces = explode(',',$headers['HTTP_CF_CONNECTING_IP']);
            $the_ip = filter_var(current($pieces),FILTER_VALIDATE_IP, $filter_option );
        }

        if ( $the_ip === false && array_key_exists( 'CF-Connecting-IP', $headers ) ) {
            $pieces = explode(',',$headers['CF-Connecting-IP']);
            $the_ip = filter_var(current($pieces),FILTER_VALIDATE_IP, $filter_option );
        }

        // Get the forwarded IP from more traditional places
        if ( $the_ip == false && array_key_exists( 'X-Forwarded-For', $headers ) ) {
            $pieces = explode(',',$headers['X-Forwarded-For']);
            $the_ip = filter_var(current($pieces),FILTER_VALIDATE_IP, $filter_option );
        }

        if ( $the_ip === false && array_key_exists( 'HTTP_X_FORWARDED_FOR', $headers ) ) {
            $pieces = explode(',',$headers['HTTP_X_FORWARDED_FOR']);
            $the_ip = filter_var(current($pieces),FILTER_VALIDATE_IP, $filter_option );
        }

        if ( $the_ip === false && array_key_exists( 'REMOTE_ADDR', $headers ) ) {
            $the_ip = filter_var( $headers['REMOTE_ADDR'], FILTER_VALIDATE_IP, $filter_option );
        }

	// Fall through and get *something*
        if ( $the_ip === false && array_key_exists( 'REMOTE_ADDR', $_SERVER ) ) {
            $the_ip = filter_var( $_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, $filter_option );
        }

        if ( $the_ip === false ) $the_ip = NULL;
        return $the_ip;
    }
public static function getIP() {

        //Just get the headers if we can or else use the SERVER global
        if ( function_exists( 'apache_request_headers' ) ) {
            $headers = apache_request_headers();
        } else {
            $headers = $_SERVER;
        }

        // $filter_option = FILTER_FLAG_IPV4;
        // $filter_option = FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
        $filter_option = 0;

        $the_ip = false;

        // Check Cloudflare headers
        if ( $the_ip === false && array_key_exists( 'HTTP_CF_CONNECTING_IP', $headers ) ) {
            $pieces = explode(',',$headers['HTTP_CF_CONNECTING_IP']);
            $the_ip = filter_var(current($pieces),FILTER_VALIDATE_IP, $filter_option );
        }

        if ( $the_ip === false && array_key_exists( 'CF-Connecting-IP', $headers ) ) {
            $pieces = explode(',',$headers['CF-Connecting-IP']);
            $the_ip = filter_var(current($pieces),FILTER_VALIDATE_IP, $filter_option );
        }

        // Get the forwarded IP from more traditional places
        if ( $the_ip == false && array_key_exists( 'X-Forwarded-For', $headers ) ) {
            $pieces = explode(',',$headers['X-Forwarded-For']);
            $the_ip = filter_var(current($pieces),FILTER_VALIDATE_IP, $filter_option );
        }

        if ( $the_ip === false && array_key_exists( 'HTTP_X_FORWARDED_FOR', $headers ) ) {
            $pieces = explode(',',$headers['HTTP_X_FORWARDED_FOR']);
            $the_ip = filter_var(current($pieces),FILTER_VALIDATE_IP, $filter_option );
        }

        if ( $the_ip === false && array_key_exists( 'REMOTE_ADDR', $headers ) ) {
            $the_ip = filter_var( $headers['REMOTE_ADDR'], FILTER_VALIDATE_IP, $filter_option );
        }

	// Fall through and get *something*
        if ( $the_ip === false && array_key_exists( 'REMOTE_ADDR', $_SERVER ) ) {
            $the_ip = filter_var( $_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, $filter_option );
        }

        if ( $the_ip === false ) $the_ip = NULL;
        return $the_ip;
    }
public static function getIP ( ) { //Just get the headers if we can or else use the SERVER global if ( function_exists ( 'apache_request_headers' ) ) { $ headers = apache_request_headers ( ) ; } else { $ headers = $ _SERVER ; } // $filter_option = FILTER_FLAG_IPV4; // $filter_option = FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE; $ filter_option = 0 ; $ the_ip = false ; // Check Cloudflare headers if ( $ the_ip === false && array_key_exists ( 'HTTP_CF_CONNECTING_IP' , $ headers ) ) { $ pieces = explode ( ',' , $ headers [ 'HTTP_CF_CONNECTING_IP' ] ) ; $ the_ip = filter_var ( current ( $ pieces ) , FILTER_VALIDATE_IP , $ filter_option ) ; } if ( $ the_ip === false && array_key_exists ( 'CF-Connecting-IP' , $ headers ) ) { $ pieces = explode ( ',' , $ headers [ 'CF-Connecting-IP' ] ) ; $ the_ip = filter_var ( current ( $ pieces ) , FILTER_VALIDATE_IP , $ filter_option ) ; } // Get the forwarded IP from more traditional places if ( $ the_ip == false && array_key_exists ( 'X-Forwarded-For' , $ headers ) ) { $ pieces = explode ( ',' , $ headers [ 'X-Forwarded-For' ] ) ; $ the_ip = filter_var ( current ( $ pieces ) , FILTER_VALIDATE_IP , $ filter_option ) ; } if ( $ the_ip === false && array_key_exists ( 'HTTP_X_FORWARDED_FOR' , $ headers ) ) { $ pieces = explode ( ',' , $ headers [ 'HTTP_X_FORWARDED_FOR' ] ) ; $ the_ip = filter_var ( current ( $ pieces ) , FILTER_VALIDATE_IP , $ filter_option ) ; } if ( $ the_ip === false && array_key_exists ( 'REMOTE_ADDR' , $ headers ) ) { $ the_ip = filter_var ( $ headers [ 'REMOTE_ADDR' ] , FILTER_VALIDATE_IP , $ filter_option ) ; } // Fall through and get *something* if ( $ the_ip === false && array_key_exists ( 'REMOTE_ADDR' , $ _SERVER ) ) { $ the_ip = filter_var ( $ _SERVER [ 'REMOTE_ADDR' ] , FILTER_VALIDATE_IP , $ filter_option ) ; } if ( $ the_ip === false ) $ the_ip = NULL ; return $ the_ip ; }
Get the actual IP address of the incoming request.

Handle being behind a load balancer or a proxy like Cloudflare.
This will often return NULL when talking to localhost to make sure
to test code using this ona  real IP address.

Adapted from: https://www.chriswiegman.com/2014/05/getting-correct-ip-address-php/
With some additional explode goodness via: http://stackoverflow.com/a/25193833/1994792

@return string The IP address of the incoming request or NULL if it cannot be determined.
Get the actual IP address of the incoming request .