简体   繁体   中英

How to compare a range of local ip addresses to a public ip address in php?

How can I compare a range of local ip address to public ip address in php?

I would like to compare my IP adddress in Range or not. If it isn't in range, I want to echo "fail connected"?

$ipRanges = array(
  array( '10.1.1.1' , '10.1.10.255' ) ,
  array( '192.168.12.1' , '192.168.12.16' )
);


$theIP = '10.1.5.5'; # Could be $_SERVER['REMOTE_ADDR'] for accessing IP address


$theIPdec = ip2long( $theIP ); # Converts from an IP address to an integer
$inRange = false;
foreach( $ipRanges as $r ){
  if( $theIPdec >= ip2long( $r[0] )
      && $theIPdec <= ip2long( $r[1] ) ){
   # IP is in this range
    $inRange = true;
    break;
  }
}


if( !$inRange ){
 # No Matches to Ranges
  echo 'fail connected';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM