Add method to get real IP from CloudFlare headers

This commit is contained in:
Kijin Sung 2016-02-02 09:30:00 +09:00
parent c23ebdb4aa
commit 343ad39b79
3 changed files with 47 additions and 23 deletions

View file

@ -91,6 +91,29 @@ class IpFilter
return false;
}
/**
* Get real IP from CloudFlare headers.
*
* @return string|false
*/
public static function getCloudFlareRealIP()
{
if (!isset($_SERVER['HTTP_CF_CONNECTING_IP']))
{
return false;
}
$cloudflare_ranges = (include RX_BASEDIR . 'common/defaults/cloudflare.php');
foreach ($cloudflare_ranges as $cloudflare_range)
{
if (self::inRange($_SERVER['REMOTE_ADDR'], $cloudflare_range))
{
return $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
}
return false;
}
/**
* Check whether the given IPv4 address belongs to a IPv4 CIDR range with mask.
*