Created
October 30, 2012 07:11
-
-
Save WordpressDev/3978746 to your computer and use it in GitHub Desktop.
codeigniter / cloudflare
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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