Skip to content

Instantly share code, notes, and snippets.

@olimortimer
Last active January 1, 2021 11:52
Show Gist options
  • Select an option

  • Save olimortimer/92ce29904a0686c2345d to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

wzul commented Aug 1, 2017

Thanks! How about if the IP is V6?

@Patrick-1994-
Copy link
Copy Markdown

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
Copy Markdown

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