note 41576 deleted from function.ip2long by cmb

From: Date: Sun, 29 Sep 2019 07:38:49 +0000
Subject: note 41576 deleted from function.ip2long by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212407@lists.php.net to get a copy of this message
Note Submitter: dzver ---- Mix of rbsmith's function and pasted above url: <?php // returns 1 if $ip is part of $network function IP_Match($network, $ip) { $ip_arr = explode("/",$network); $network_long=ip2long($ip_arr[0]); $mask_long= pow(2,32)-pow(2,(32-$ip_arr[1])); $ip_long=ip2long($ip); if (($ip_long & $mask_long) == $network_long) { return 1; } else { return 0; } } // usage $network="200.100.50.0/23"; $ip="200.100.51.55"; $ip2="200.100.52.2"; echo IP_Match($network, $ip); //prints 1 echo IP_Match($network, $ip2); //prints 0 ?>

« previous php.notes (#212407) next »