3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Check if a given ip is in a network * @param string $ip IP to check in IPV4 format eg. 127.0.0.1 * @param string $range IP/CIDR netmask eg. 127.0.0.0/24, also 127.0.0.1 is accepted and /32 assumed * @return boolean true if the ip is in this range / false if not. */ function ip_in_range( $ip, $range ) { if ( strpos( $range, '/' ) == false ) { $range .= '/32'; } // $range is in IP/CIDR format eg 127.0.0.1/24 list( $range, $netmask ) = explode( '/', $range, 2 ); $range_decimal = ip2long( $range ); $ip_decimal = ip2long( $ip ); $wildcard_decimal = pow( 2, ( 32 - $netmask ) ) - 1; $netmask_decimal = ~ $wildcard_decimal; return ( ( $ip_decimal & $netmask_decimal ) == ( $range_decimal & $netmask_decimal ) ); } var_dump(ip_in_range("204.15.21.0", "204.15.20.0/22"));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/susSb
function name:  (null)
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'ip_in_range'
          2        SEND_VAL                                                 '204.15.21.0'
          3        SEND_VAL                                                 '204.15.20.0%2F22'
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function ip_in_range:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/susSb
function name:  ip_in_range
number of ops:  41
compiled vars:  !0 = $ip, !1 = $range, !2 = $netmask, !3 = $range_decimal, !4 = $ip_decimal, !5 = $wildcard_decimal, !6 = $netmask_decimal
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   11     2        INIT_FCALL                                               'strpos'
          3        SEND_VAR                                                 !1
          4        SEND_VAL                                                 '%2F'
          5        DO_ICALL                                         $7      
          6        BOOL_NOT                                         ~8      $7
          7      > JMPZ                                                     ~8, ->9
   12     8    >   ASSIGN_OP                                     8          !1, '%2F32'
   15     9    >   INIT_FCALL                                               'explode'
         10        SEND_VAL                                                 '%2F'
         11        SEND_VAR                                                 !1
         12        SEND_VAL                                                 2
         13        DO_ICALL                                         $10     
         14        FETCH_LIST_R                                     $11     $10, 0
         15        ASSIGN                                                   !1, $11
         16        FETCH_LIST_R                                     $13     $10, 1
         17        ASSIGN                                                   !2, $13
         18        FREE                                                     $10
   16    19        INIT_FCALL                                               'ip2long'
         20        SEND_VAR                                                 !1
         21        DO_ICALL                                         $15     
         22        ASSIGN                                                   !3, $15
   17    23        INIT_FCALL                                               'ip2long'
         24        SEND_VAR                                                 !0
         25        DO_ICALL                                         $17     
         26        ASSIGN                                                   !4, $17
   18    27        INIT_FCALL                                               'pow'
         28        SEND_VAL                                                 2
         29        SUB                                              ~19     32, !2
         30        SEND_VAL                                                 ~19
         31        DO_ICALL                                         $20     
         32        SUB                                              ~21     $20, 1
         33        ASSIGN                                                   !5, ~21
   19    34        BW_NOT                                           ~23     !5
         35        ASSIGN                                                   !6, ~23
   20    36        BW_AND                                           ~25     !4, !6
         37        BW_AND                                           ~26     !3, !6
         38        IS_EQUAL                                         ~27     ~25, ~26
         39      > RETURN                                                   ~27
   21    40*     > RETURN                                                   null

End of function ip_in_range

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.06 ms | 1403 KiB | 24 Q