3v4l.org

run code in 300+ PHP versions simultaneously
<?php // simple example $bcast = ip2long("192.168.178.255"); $smask = ip2long("255.255.255.0"); $nmask = $bcast & $smask; echo long2ip($nmask); // Will give 192.168.178.0 ?> With this example you are able to check if a given host is in your own local net or not (on linux): <?php /** * Check if a client IP is in our Server subnet * * @param string $client_ip * @param string $server_ip * @return boolean */ function clientInSameSubnet($client_ip=false,$server_ip=false) { if (!$client_ip) $client_ip = $_SERVER['REMOTE_ADDR']; if (!$server_ip) $server_ip = $_SERVER['SERVER_ADDR']; // Extract broadcast and netmask from ifconfig if (!($p = popen("ifconfig","r"))) return false; $out = ""; while(!feof($p)) $out .= fread($p,1024); fclose($p); // This is because the php.net comment function does not // allow long lines. $match = "/^.*".$server_ip; $match .= ".*Bcast:(\d{1,3}\.\d{1,3}i\.\d{1,3}\.\d{1,3}).*"; $match .= "Mask:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/im"; if (!preg_match($match,$out,$regs)) return false; $bcast = ip2long($regs[1]); $smask = ip2long($regs[2]); $ipadr = ip2long($client_ip); $nmask = $bcast & $smask; return (($ipadr & $smask) == ($nmask & $smask)); } ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/35mNZ
function name:  (null)
number of ops:  16
compiled vars:  !0 = $bcast, !1 = $smask, !2 = $nmask
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'ip2long'
          1        SEND_VAL                                                 '192.168.178.255'
          2        DO_ICALL                                         $3      
          3        ASSIGN                                                   !0, $3
    4     4        INIT_FCALL                                               'ip2long'
          5        SEND_VAL                                                 '255.255.255.0'
          6        DO_ICALL                                         $5      
          7        ASSIGN                                                   !1, $5
    5     8        BW_AND                                           ~7      !0, !1
          9        ASSIGN                                                   !2, ~7
    6    10        INIT_FCALL                                               'long2ip'
         11        SEND_VAR                                                 !2
         12        DO_ICALL                                         $9      
         13        ECHO                                                     $9
    8    14        ECHO                                                     '%0AWith+this+example+you+are+able+to+check+if+a+given+host+is+in+your+own+local+net+or+not+%28on+linux%29%3A%0A%0A'
   43    15      > RETURN                                                   1

Function clientinsamesubnet:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 7
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 12
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 20
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 22
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 47
Branch analysis from position: 46
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 22
Branch analysis from position: 32
Branch analysis from position: 22
Branch analysis from position: 12
Branch analysis from position: 7
filename:       /in/35mNZ
function name:  clientInSameSubnet
number of ops:  68
compiled vars:  !0 = $client_ip, !1 = $server_ip, !2 = $p, !3 = $out, !4 = $match, !5 = $regs, !6 = $bcast, !7 = $smask, !8 = $ipadr, !9 = $nmask
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV_INIT                                        !0      <false>
          1        RECV_INIT                                        !1      <false>
   20     2        BOOL_NOT                                         ~10     !0
          3      > JMPZ                                                     ~10, ->7
   21     4    >   FETCH_R                      global              ~11     '_SERVER'
          5        FETCH_DIM_R                                      ~12     ~11, 'REMOTE_ADDR'
          6        ASSIGN                                                   !0, ~12
   22     7    >   BOOL_NOT                                         ~14     !1
          8      > JMPZ                                                     ~14, ->12
   23     9    >   FETCH_R                      global              ~15     '_SERVER'
         10        FETCH_DIM_R                                      ~16     ~15, 'SERVER_ADDR'
         11        ASSIGN                                                   !1, ~16
   25    12    >   INIT_FCALL                                               'popen'
         13        SEND_VAL                                                 'ifconfig'
         14        SEND_VAL                                                 'r'
         15        DO_ICALL                                         $18     
         16        ASSIGN                                           ~19     !2, $18
         17        BOOL_NOT                                         ~20     ~19
         18      > JMPZ                                                     ~20, ->20
         19    > > RETURN                                                   <false>
   26    20    >   ASSIGN                                                   !3, ''
   27    21      > JMP                                                      ->27
   28    22    >   INIT_FCALL                                               'fread'
         23        SEND_VAR                                                 !2
         24        SEND_VAL                                                 1024
         25        DO_ICALL                                         $22     
         26        ASSIGN_OP                                     8          !3, $22
   27    27    >   INIT_FCALL                                               'feof'
         28        SEND_VAR                                                 !2
         29        DO_ICALL                                         $24     
         30        BOOL_NOT                                         ~25     $24
         31      > JMPNZ                                                    ~25, ->22
   29    32    >   INIT_FCALL                                               'fclose'
         33        SEND_VAR                                                 !2
         34        DO_ICALL                                                 
   32    35        CONCAT                                           ~27     '%2F%5E.%2A', !1
         36        ASSIGN                                                   !4, ~27
   33    37        ASSIGN_OP                                     8          !4, '.%2ABcast%3A%28%5Cd%7B1%2C3%7D%5C.%5Cd%7B1%2C3%7Di%5C.%5Cd%7B1%2C3%7D%5C.%5Cd%7B1%2C3%7D%29.%2A'
   34    38        ASSIGN_OP                                     8          !4, 'Mask%3A%28%5Cd%7B1%2C3%7D%5C.%5Cd%7B1%2C3%7D%5C.%5Cd%7B1%2C3%7D%5C.%5Cd%7B1%2C3%7D%29%24%2Fim'
   35    39        INIT_FCALL                                               'preg_match'
         40        SEND_VAR                                                 !4
         41        SEND_VAR                                                 !3
         42        SEND_REF                                                 !5
         43        DO_ICALL                                         $31     
         44        BOOL_NOT                                         ~32     $31
         45      > JMPZ                                                     ~32, ->47
   36    46    > > RETURN                                                   <false>
   37    47    >   INIT_FCALL                                               'ip2long'
         48        FETCH_DIM_R                                      ~33     !5, 1
         49        SEND_VAL                                                 ~33
         50        DO_ICALL                                         $34     
         51        ASSIGN                                                   !6, $34
   38    52        INIT_FCALL                                               'ip2long'
         53        FETCH_DIM_R                                      ~36     !5, 2
         54        SEND_VAL                                                 ~36
         55        DO_ICALL                                         $37     
         56        ASSIGN                                                   !7, $37
   39    57        INIT_FCALL                                               'ip2long'
         58        SEND_VAR                                                 !0
         59        DO_ICALL                                         $39     
         60        ASSIGN                                                   !8, $39
   40    61        BW_AND                                           ~41     !6, !7
         62        ASSIGN                                                   !9, ~41
   41    63        BW_AND                                           ~43     !8, !7
         64        BW_AND                                           ~44     !9, !7
         65        IS_EQUAL                                         ~45     ~43, ~44
         66      > RETURN                                                   ~45
   42    67*     > RETURN                                                   null

End of function clientinsamesubnet

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
168.99 ms | 1408 KiB | 27 Q