Last active
January 1, 2021 11:52
-
-
Save olimortimer/92ce29904a0686c2345d to your computer and use it in GitHub Desktop.
PHP: Check IP is 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
<?php | |
function cidr_match($ip) { | |
foreach(file('https://www.cloudflare.com/ips-v4') as $cidr) { | |
list($subnet, $mask) = explode('/', $cidr); | |
if ((ip2long($ip) & ~((1 << (32 - $mask)) - 1) ) == ip2long($subnet)) { | |
return true; | |
} | |
continue; | |
} | |
return false; | |
} |
I got an error about a malformed number (or something similar). I had to use
foreach( array_map("trim", file('https://www.cloudflare.com/ips-v4')) as $cidr) {
Thank you, working fine
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! How about if the IP is V6?