note 63778 added to function.ip2long

From: Date: Thu, 30 Mar 2006 14:15:07 +0000
Subject: note 63778 added to function.ip2long
Groups: php.notes 
Request: Send a blank email to php-notes+get-107032@lists.php.net to get a copy of this message
Here is a script that will calculate host range and number of hosts with a given ip address CIDR notation (modified code which was posted by phl AT cyways.com): $ip_addr_cidr = "192.168.37.215/27"; $ip_arr = explode('/', $ip_addr_cidr); $bin = ''; for($i=1;$i<=32;$i++) { $bin .= $ip_arr[1] >= $i ? '1' : '0'; } $ip_arr[1] = bindec($bin); $ip = ip2long($ip_arr[0]); $nm = ip2long($ip_arr[1]); $nw = ($ip & $nm); $bc = $nw | (~$nm); echo "Number of Hosts: " . ($bc - $nw - 1) . "\n"; echo "Host Range: " . long2ip($nw + 1) . " -> " . long2ip($bc - 1) . "\n"; This will produce: Number of Hosts: 30 Host Range: 192.168.37.193 -> 192.168.37.222 ---- Server IP: 80.81.171.181 Probable Submitter: 213.243.160.111 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.ip2long.php Edit -- http://master.php.net/note/edit/63778 Del: integrated -- http://master.php.net/note/delete/63778/integrated Del: useless -- http://master.php.net/note/delete/63778/useless Del: bad code -- http://master.php.net/note/delete/63778/bad+code Del: spam -- http://master.php.net/note/delete/63778/spam Del: non-english -- http://master.php.net/note/delete/63778/non-english Del: in docs -- http://master.php.net/note/delete/63778/in+docs Del: other reasons-- http://master.php.net/note/delete/63778 Reject -- http://master.php.net/note/reject/63778 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#107032) next »