3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* * ip_in_range.php - Function to determine if an IP is located in a * specific range as specified via several alternative * formats. * * Network ranges can be specified as: * 1. Wildcard format: 1.2.3.* * 2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0 * 3. Start-End IP format: 1.2.3.0-1.2.3.255 * * Return value BOOLEAN : ip_in_range($ip, $range); * * Copyright 2008: Paul Gregg <pgregg@pgregg.com> * 10 January 2008 * Version: 1.2 * * Source website: http://www.pgregg.com/projects/php/ip_in_range/ * Version 1.2 * * This software is Donationware - if you feel you have benefited from * the use of this tool then please consider a donation. The value of * which is entirely left up to your discretion. * http://www.pgregg.com/donate/ * * Please do not remove this header, or source attibution from this file. */ /* * Modified by James Greene <james@cloudflare.com> to include IPV6 support * (original version only supported IPV4). * 21 May 2012 */ // decbin32 // In order to simplify working with IP addresses (in binary) and their // netmasks, it is easier to ensure that the binary strings are padded // with zeros out to 32 characters - IP addresses are 32 bit numbers function decbin32 ($dec) { return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT); } // ipv4_in_range // This function takes 2 arguments, an IP address and a "range" in several // different formats. // Network ranges can be specified as: // 1. Wildcard format: 1.2.3.* // 2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0 // 3. Start-End IP format: 1.2.3.0-1.2.3.255 // The function will return true if the supplied IP is within the range. // Note little validation is done on the range inputs - it expects you to // use one of the above 3 formats. function ipv4_in_range($ip, $range) { if (strpos($range, '/') !== false) { // $range is in IP/NETMASK format list($range, $netmask) = explode('/', $range, 2); if (strpos($netmask, '.') !== false) { // $netmask is a 255.255.0.0 format $netmask = str_replace('*', '0', $netmask); $netmask_dec = ip2long($netmask); return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) ); } else { // $netmask is a CIDR size block // fix the range argument $x = explode('.', $range); while(count($x)<4) $x[] = '0'; list($a,$b,$c,$d) = $x; $range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d); $range_dec = ip2long($range); $ip_dec = ip2long($ip); # Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s #$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0')); # Strategy 2 - Use math to create it $wildcard_dec = pow(2, (32-$netmask)) - 1; $netmask_dec = ~ $wildcard_dec; return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec)); } } else { // range might be 255.255.*.* or 1.2.3.0-1.2.3.255 if (strpos($range, '*') !==false) { // a.b.*.* format // Just convert to A-B format by setting * to 0 for A and 255 for B $lower = str_replace('*', '0', $range); $upper = str_replace('*', '255', $range); $range = "$lower-$upper"; } if (strpos($range, '-')!==false) { // A-B format list($lower, $upper) = explode('-', $range, 2); $lower_dec = (float)sprintf("%u",ip2long($lower)); $upper_dec = (float)sprintf("%u",ip2long($upper)); $ip_dec = (float)sprintf("%u",ip2long($ip)); return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) ); } return false; } } function ip2long6($ip) { if (substr_count($ip, '::')) { $ip = str_replace('::', str_repeat(':0000', 8 - substr_count($ip, ':')) . ':', $ip); } $ip = explode(':', $ip); $r_ip = ''; foreach ($ip as $v) { $r_ip .= str_pad(base_convert($v, 16, 2), 16, 0, STR_PAD_LEFT); } return base_convert($r_ip, 2, 10); } // Get the ipv6 full format and return it as a decimal value. function get_ipv6_full($ip) { $pieces = explode ("/", $ip, 2); $left_piece = $pieces[0]; $right_piece = $pieces[1]; // Extract out the main IP pieces $ip_pieces = explode("::", $left_piece, 2); $main_ip_piece = $ip_pieces[0]; $last_ip_piece = $ip_pieces[1]; // Pad out the shorthand entries. $main_ip_pieces = explode(":", $main_ip_piece); foreach($main_ip_pieces as $key=>$val) { $main_ip_pieces[$key] = str_pad($main_ip_pieces[$key], 4, "0", STR_PAD_LEFT); } // Check to see if the last IP block (part after ::) is set $last_piece = ""; $size = count($main_ip_pieces); if (trim($last_ip_piece) != "") { $last_piece = str_pad($last_ip_piece, 4, "0", STR_PAD_LEFT); // Build the full form of the IPV6 address considering the last IP block set for ($i = $size; $i < 7; $i++) { $main_ip_pieces[$i] = "0000"; } $main_ip_pieces[7] = $last_piece; } else { // Build the full form of the IPV6 address for ($i = $size; $i < 8; $i++) { $main_ip_pieces[$i] = "0000"; } } // Rebuild the final long form IPV6 address $final_ip = implode(":", $main_ip_pieces); return ip2long6($final_ip); } // Determine whether the IPV6 address is within range. // $ip is the IPV6 address in decimal format to check if its within the IP range created by the cloudflare IPV6 address, $range_ip. // $ip and $range_ip are converted to full IPV6 format. // Returns true if the IPV6 address, $ip, is within the range from $range_ip. False otherwise. function ipv6_in_range($ip, $range_ip) { $pieces = explode ("/", $range_ip, 2); $left_piece = $pieces[0]; $right_piece = $pieces[1]; // Extract out the main IP pieces $ip_pieces = explode("::", $left_piece, 2); $main_ip_piece = $ip_pieces[0]; $last_ip_piece = $ip_pieces[1]; // Pad out the shorthand entries. $main_ip_pieces = explode(":", $main_ip_piece); foreach($main_ip_pieces as $key=>$val) { $main_ip_pieces[$key] = str_pad($main_ip_pieces[$key], 4, "0", STR_PAD_LEFT); } // Create the first and last pieces that will denote the IPV6 range. $first = $main_ip_pieces; $last = $main_ip_pieces; // Check to see if the last IP block (part after ::) is set $last_piece = ""; $size = count($main_ip_pieces); if (trim($last_ip_piece) != "") { $last_piece = str_pad($last_ip_piece, 4, "0", STR_PAD_LEFT); // Build the full form of the IPV6 address considering the last IP block set for ($i = $size; $i < 7; $i++) { $first[$i] = "0000"; $last[$i] = "ffff"; } $main_ip_pieces[7] = $last_piece; } else { // Build the full form of the IPV6 address for ($i = $size; $i < 8; $i++) { $first[$i] = "0000"; $last[$i] = "ffff"; } } // Rebuild the final long form IPV6 address $first = ip2long6(implode(":", $first)); $last = ip2long6(implode(":", $last)); $in_range = ($ip >= $first && $ip <= $last); return $in_range; } function ip_range_type($range_ip) { if (strpos($range_ip, ':') !== false) { //if ip range provided is ipv6 then //echo "range provided is ipv6"; return 1; } else { //if ip range provided is ipv4 then //echo "range provided is ipv4"; return 2; } } function ip_in_range($ip, $range_ip) { //only check IPv4 addresses against IPv4 ranges if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) && ip_range_type($range_ip) == 2) { return ipv4_in_range($ip, $range_ip); } //only check IPv6 addresses against IPv6 ranges if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && ip_range_type($range_ip) == 1) { return ipv6_in_range($ip, $range_ip); } } $ip = "173.224.116.233"; $range_ip = /*"173.224.117.0/24";*/"173.224.112.0/20"; $ip = "2a02:c7f:9e03:1d00:59c2:3083:c23b:7146"; $range_ip = "2a02:c78::/29"; echo "Is IP in range : " . ip_in_range($ip, $range_ip); //echo ip_range_type($range_ip);

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.5.20.0100.00819.46
8.5.10.0090.00816.79
8.5.00.0100.01120.02
8.4.170.0080.01424.06
8.4.160.0140.00920.64
8.4.150.0020.00014.05
8.4.140.0120.01217.68
8.4.130.0070.00317.64
8.4.120.0140.00621.00
8.4.110.0110.01122.53
8.4.100.0130.00917.84
8.4.90.0090.00618.97
8.4.80.0130.00717.87
8.4.70.0130.00818.00
8.4.60.0110.01118.75
8.4.50.0110.01119.73
8.4.40.0080.00317.79
8.4.30.0100.01017.67
8.4.20.0100.01019.39
8.4.10.0070.01319.57
8.3.300.0090.01220.93
8.3.290.0090.01120.93
8.3.280.0130.00918.45
8.3.270.0140.00717.13
8.3.260.0090.00916.70
8.3.250.0120.00819.30
8.3.240.0130.00716.80
8.3.230.0090.00716.84
8.3.220.0070.01117.66
8.3.210.0120.00816.78
8.3.200.0050.00416.79
8.3.190.0040.00517.12
8.3.180.0100.00916.86
8.3.170.0120.00620.59
8.3.160.0090.00317.03
8.3.150.0030.01519.09
8.3.140.0060.01216.98
8.3.130.0040.00418.75
8.3.120.0060.00319.09
8.3.110.0090.00020.94
8.3.100.0060.01324.06
8.3.90.0060.00926.77
8.3.80.0060.00316.75
8.3.70.0130.00316.75
8.3.60.0120.00316.75
8.3.50.0150.00217.72
8.3.40.0090.00619.09
8.3.30.0060.00918.84
8.3.20.0040.00420.59
8.3.10.0040.00423.73
8.3.00.0000.00823.78
8.2.300.0100.00722.19
8.2.290.0130.00620.45
8.2.280.0030.00718.39
8.2.270.0160.00317.29
8.2.260.0150.00318.72
8.2.250.0030.00516.90
8.2.240.0090.00619.12
8.2.230.0040.00422.58
8.2.220.0040.00437.54
8.2.210.0110.00726.77
8.2.200.0090.00016.63
8.2.190.0160.00316.88
8.2.180.0070.00718.54
8.2.170.0060.01219.34
8.2.160.0150.00022.96
8.2.150.0050.00325.66
8.2.140.0050.00324.66
8.2.130.0000.00822.55
8.2.120.0050.00326.35
8.2.110.0000.00920.73
8.2.100.0070.00418.16
8.2.90.0030.00618.00
8.2.80.0030.00619.17
8.2.70.0050.00517.88
8.2.60.0040.00418.00
8.2.50.0040.00418.03
8.2.40.0100.00019.60
8.2.30.0000.00819.50
8.2.20.0000.01018.45
8.2.10.0080.00018.37
8.2.00.0000.00818.26
8.1.340.0130.00721.72
8.1.330.0130.00721.87
8.1.320.0160.00516.38
8.1.310.0040.01418.47
8.1.300.0110.00016.50
8.1.290.0030.00618.88
8.1.280.0130.00725.92
8.1.270.0080.00022.30
8.1.260.0000.00826.35
8.1.250.0060.00328.09
8.1.240.0030.00622.22
8.1.230.0040.00818.08
8.1.220.0000.00818.16
8.1.210.0000.00818.77
8.1.200.0000.01017.72
8.1.190.0030.00717.60
8.1.180.0030.00718.10
8.1.170.0000.00817.75
8.1.160.0070.00419.25
8.1.150.0000.00919.14
8.1.140.0050.00217.93
8.1.130.0030.00619.13
8.1.120.0000.00817.58
8.1.110.0040.00417.74
8.1.100.0040.00417.75
8.1.90.0070.00017.75
8.1.80.0060.00317.75
8.1.70.0070.00017.73
8.1.60.0040.00417.88
8.1.50.0050.00317.81
8.1.40.0140.00417.65
8.1.30.0040.00417.80
8.1.20.0080.00017.89
8.1.10.0000.00817.84
8.1.00.0070.00317.82
8.0.300.0040.00419.24
8.0.290.0080.00016.88
8.0.280.0030.00318.67
8.0.270.0040.00416.95
8.0.260.0000.00720.78
8.0.250.0080.00017.05
8.0.240.0000.00717.18
8.0.230.0070.00017.06
8.0.220.0040.00417.12
8.0.210.0070.00017.04
8.0.200.0080.00017.18
8.0.190.0040.00417.09
8.0.180.0000.00817.12
8.0.170.0000.00817.22
8.0.160.0070.00017.17
8.0.150.0050.00216.97
8.0.140.0050.00317.08
8.0.130.0000.00613.60
8.0.120.0050.00317.03
8.0.110.0070.00017.15
8.0.100.0000.00817.14
8.0.90.0040.00417.01
8.0.80.0140.00717.12
8.0.70.0000.00817.26
8.0.60.0000.00817.25
8.0.50.0000.00817.00
8.0.30.0110.00817.49
8.0.20.0100.01117.47
8.0.10.0030.00717.37
8.0.00.0070.01117.21
7.4.330.0000.00616.93
7.4.320.0000.00716.78
7.4.300.0070.00016.92
7.4.290.0080.00016.84
7.4.280.0000.00716.83
7.4.270.0080.00016.85
7.4.260.0060.00316.77
7.4.250.0040.00416.90
7.4.240.0000.00716.89
7.4.230.0040.00416.55
7.4.220.0130.00616.80
7.4.210.0100.00616.91
7.4.200.0000.00716.63
7.4.160.0070.01116.59
7.4.150.0090.01317.40
7.4.140.0110.01117.86
7.4.130.0070.01116.78
7.4.120.0090.01116.69
7.4.110.0140.01016.60
7.4.100.0000.01916.70
7.4.90.0040.01516.97
7.4.80.0160.00919.39
7.4.70.0060.01516.78
7.4.60.0090.00916.69
7.4.50.0090.00616.51
7.4.40.0070.01016.70
7.4.30.0110.01116.90
7.4.00.0090.00615.35
7.3.330.0000.00513.61
7.3.320.0060.00013.61
7.3.310.0000.00716.66
7.3.300.0000.00816.49
7.3.290.0060.00816.56
7.3.280.0070.01216.60
7.3.270.0060.01217.40
7.3.260.0080.01016.75
7.3.250.0150.00616.60
7.3.240.0120.00716.68
7.3.230.0030.01316.84
7.3.210.0030.01316.86
7.3.200.0070.01616.79
7.3.190.0160.00016.61
7.3.180.0130.01016.91
7.3.170.0030.01316.64
7.3.160.0080.00816.55
7.2.330.0060.01217.04
7.2.320.0060.01116.89
7.2.310.0080.00916.79
7.2.300.0030.01517.11
7.2.290.0080.00917.11
7.2.60.0100.01017.24
7.1.200.0030.00915.89
7.1.110.0070.01018.46
7.1.100.0070.00718.36
7.1.90.0100.00318.09
7.1.80.0110.00818.00
7.1.70.0070.01017.21
7.1.60.0250.00635.27
7.1.50.0100.01634.96
7.1.40.0190.00934.75
7.1.30.0160.01234.66
7.1.20.0130.01334.81
7.1.10.0050.00816.81
7.1.00.0000.01516.74
7.0.250.0040.00917.70
7.0.240.0030.00917.65
7.0.230.0030.01017.68
7.0.220.0000.01617.71
7.0.210.0070.00716.87
7.0.200.0070.00716.95
7.0.190.0060.00617.02
7.0.180.0030.01116.43
7.0.170.0080.00516.30
7.0.160.0070.00716.54
7.0.150.0080.00516.13
7.0.140.0080.00516.47
7.0.130.0030.01716.70
7.0.120.0030.01716.61
7.0.110.0040.01416.43
7.0.100.0070.01016.39
7.0.90.0000.01416.59
7.0.80.0030.01016.66
7.0.70.0090.00316.65
7.0.60.0090.00416.21
7.0.50.0000.01316.61
7.0.40.0090.00316.63
7.0.30.0050.00816.67
7.0.20.0000.01316.63
7.0.10.0000.01216.46
7.0.00.0030.00916.50

preferences:
191.03 ms | 403 KiB | 5 Q