Last active
April 24, 2020 19:23
-
-
Save timint/9bf3b4d95db83403cbc0a62853fc2e48 to your computer and use it in GitHub Desktop.
Cloudflare - Set real IP
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
<?php | |
// Cloudflare - Set Real IP | |
$cloudflare_ip_ranges = array( | |
'204.93.240.0/24', '204.93.177.0/24', '199.27.128.0/21', | |
'173.245.48.0/20', '103.21.244.0/22', '103.22.200.0/22', | |
'103.31.4.0/22', '141.101.64.0/18', '108.162.192.0/18', | |
'190.93.240.0/20', '188.114.96.0/20', '197.234.240.0/22', | |
'198.41.128.0/17', '162.158.0.0/15', | |
); | |
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) { | |
foreach ($cloudflare_ip_ranges as $cidr) { | |
list($subnet, $mask) = explode('/', $cidr); | |
if ((ip2long($_SERVER['REMOTE_ADDR']) & ($mask = ~ ((1 << (32 - $mask)) - 1))) == (ip2long($subnet) & $mask)) { | |
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP']; | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment