note 36532 added to function.ip2long

From: Date: Tue, 14 Oct 2003 01:23:22 +0000
Subject: note 36532 added to function.ip2long
Groups: php.notes 
Request: Send a blank email to php-notes+get-58558@lists.php.net to get a copy of this message
function CalculateIPRange($iprange) { // Daevid Vincent [daevid@daevid.com] 10.13.03 // This function will return an array of either a negative error code // or all possible IP addresses in the given range. // format is NNN.NNN.NNN.NNN - NNN.NNN.NNN.NNN (spaces are okay) $temp = preg_split("/-/",$iprange, -1, PREG_SPLIT_NO_EMPTY); $QRange1 = $temp[0]; $QRange2 = $temp[1]; if ($QRange2 == "") return array($QRange1); //special case, they didn't put a second quad parameter //basic error handling to see if it is generally a valid IP in the form N.N.N.N if ( preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/",$QRange1) != 1 ) return array(-1); if ( preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/",$QRange2) != 1 ) return array(-1); $quad1 = explode(".",$QRange1); $quad2 = explode(".",$QRange2); reset ($quad1); while (list ($key, $val) = each ($quad1)) { $quad1[$key] = intval($val); if ($quad1[$key] < 0 || $quad1[$key] > 255) return array(-2); } reset ($quad2); while (list ($key, $val) = each ($quad2)) { $quad2[$key] = intval($val); if ($quad2[$key] < 0 || $quad2[$key] > 255) return array(-2); } $startIP_long = sprintf("%u",ip2long($QRange1)); $endIP_long = sprintf("%u",ip2long($QRange2)); $difference = $endIP_long - $startIP_long; //echo "startIP_long = ".$startIP_long." and endIP_long = ".$endIP_long." difference = ".$difference."<BR>"; $ip = array(); $k = 0; for ($i = $startIP_long; $i <= $endIP_long; $i++) { $temp = long2ip($i); //this is a total hack. there must be a better way. $thisQuad = explode(".",$temp); if ($thisQuad[3] > 0 && $thisQuad[3] < 255) $ip[$k++] = $temp; } return $ip; } //CalculateIPRange() ---- Manual Page -- http://www.php.net/manual/en/function.ip2long.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+36532 Delete -- http://master.php.net/manage/user-notes.php?action=delete+36532&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+36532&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#58558) next »