function is_cloudflare_ip($ip) { // updated: 2025-04-04 // Convert IP string to number if (is_string($ip)) { $bytes = explode('.', $ip); $ip = 0; foreach ($bytes as $n) { $ip <<= 8; $ip |= (int)$n; } } // Check rules switch ($ip & 0b0_11111111_11111000_00000000_00000000) { // mask: 255.248.0.0 case 0b0_01101000_00010000_00000000_00000000: // ip: 104.16.0.0 case 0b0_10101100_01000000_00000000_00000000: // ip: 172.64.0.0 return true; } switch ($ip & 0b0_11111111_11111100_00000000_00000000) { // mask: 255.252.0.0 case 0b0_01101000_00011000_00000000_00000000: // ip: 104.24.0.0 return true; } switch ($ip & 0b0_11111111_11111110_00000000_00000000) { // mask: 255.254.0.0 case 0b0_10100010_10011110_00000000_00000000: // ip: 162.158.0.0 return true; } switch ($ip & 0b0_11111111_11111111_10000000_00000000) { // mask: 255.255.128.0 case 0b0_11000110_00101001_10000000_00000000: // ip: 198.41.128.0 return true; } switch ($ip & 0b0_11111111_11111111_11000000_00000000) { // mask: 255.255.192.0 case 0b0_01101100_10100010_11000000_00000000: // ip: 108.162.192.0 case 0b0_10001101_01100101_01000000_00000000: // ip: 141.101.64.0 return true; } switch ($ip & 0b0_11111111_11111111_11110000_00000000) { // mask: 255.255.240.0 case 0b0_10101101_11110101_00110000_00000000: // ip: 173.245.48.0 case 0b0_10111100_01110010_01100000_00000000: // ip: 188.114.96.0 case 0b0_10111110_01011101_11110000_00000000: // ip: 190.93.240.0 return true; } switch ($ip & 0b0_11111111_11111111_11111100_00000000) { // mask: 255.255.252.0 case 0b0_01100111_00010101_11110100_00000000: // ip: 103.21.244.0 case 0b0_01100111_00010110_11001000_00000000: // ip: 103.22.200.0 case 0b0_01100111_00011111_00000100_00000000: // ip: 103.31.4.0 case 0b0_10000011_00000000_01001000_00000000: // ip: 131.0.72.0 case 0b0_11000101_11101010_11110000_00000000: // ip: 197.234.240.0 return true; } return false; }