note 67531 deleted from function.ip2long by bjori
| From: | bjori@php.net | Date: | Thu, 08 Jun 2006 16:56:58 +0000 |
| Subject: | note 67531 deleted from function.ip2long by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-113854@lists.php.net to get a copy of this message | ||
Note Submitter: Gary Rogers
Reason: useless
----
Here is a function that will generate a random ip address by class (A, B, C, D, or E) and can also
generate addresses in the private address scheme for each class:
function random_ip($class = 'A', $private = false){
$ip = array();
// handle the ip class
switch ($class){
case 'A':
default:
$ip[0] = (!$private) ? rand(1, 126) : 10;
$ip[1] = rand(0, 255);
$ip[2] = rand(0, 255);
$ip[3] = rand(1, 254);
break;
case 'B':
$ip[0] = (!$private) ? rand(128, 191) : 172;
$ip[1] = (!$private) ? rand(1, 255) : rand(16, 31);
$ip[2] = rand(0, 255);
$ip[3] = rand(1, 254);
break;
case 'C':
$ip[0] = (!$private) ? rand(192, 223) : 192;
$ip[1] = (!$private) ? rand(0, 255) : 168;
$ip[2] = rand(1, 254);
$ip[3] = rand(1, 254);
break;
case 'D':
$ip[0] = rand(224, 293);
$ip[1] = rand(0, 255);
$ip[2] = rand(0, 255);
$ip[3] = rand(0, 255);
break;
case 'E':
$ip[0] = rand(240, 254);
$ip[1] = rand(0, 255);
$ip[2] = rand(0, 255);
$ip[3] = rand(0, 254);
break;
}
return implode('.', $ip);
}