Skip to content

Instantly share code, notes, and snippets.

@olimortimer
Last active January 1, 2021 11:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olimortimer/92ce29904a0686c2345d to your computer and use it in GitHub Desktop.
Save olimortimer/92ce29904a0686c2345d to your computer and use it in GitHub Desktop.
PHP: Check IP is CloudFlare
<?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;
}
@wzul
Copy link

wzul commented Aug 1, 2017

Thanks! How about if the IP is V6?

@Patrick-1994-
Copy link

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) {

@fnzv
Copy link

fnzv commented Jan 1, 2021

Thank you, working fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment