Skip to content

Instantly share code, notes, and snippets.

@WordpressDev
Created October 30, 2012 07:11
Show Gist options
  • Save WordpressDev/3978746 to your computer and use it in GitHub Desktop.
Save WordpressDev/3978746 to your computer and use it in GitHub Desktop.
codeigniter / cloudflare
class MY_Input extends CI_Input
{
function ip_address()
{
$ip = parent::ip_address();
$proxy = $this->get_request_header('X-Forwarded-For');
if($proxy) // check if there is a proxy being used
{
$iplong = ip2long($ip);
// list of IP => mask
/// http://www.cloudflare.com/wiki/What_are_the_CloudFlare_IP_address_ranges
$whitelist = array(
'204.93.240.0' => 24,
'204.93.177.0' => 24,
'199.27.128.0' => 21,
'173.245.48.0' => 20,
'103.22.200.0' => 22,
'141.101.64.0' => 18
);
foreach($whitelist as $whiteip => $mask)
{
$bitmask = bindec(str_repeat('1', $mask) . str_repeat('0', 32 - $mask));
$trusted = ip2long($whiteip);
if(($iplong & $bitmask) == $trusted)
{
$ip = $proxy;
break;
}
}
}
return $ip;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment