在 RANGE 中生成 IP
public static function generateRandomIp($start, $end) {
if (strcmp($start, $end) > 0) {
return false;
}
$arrStart = explode('.',$start);
$arrEnd = explode('.', $end);
// First
$arrIp[0] = rand($arrStart[0], $arrEnd[0]);
// Second
if ($arrIp[0] == $arrStart[0] && $arrIp[0] == $arrEnd[0]) {
$arrIp[1] = rand($arrStart[1], $arrEnd[1]);
} elseif ($arrIp[0] == $arrStart[0]) {
$arrIp[1] = rand($arrStart[1], 255);
} elseif ($arrIp[0] == $arrEnd[0]) {
$arrIp[1] = rand(0, $arrEnd[1]);
} else {
$arrIp[1] = rand(0, 255);
}
// Third
if ($arrIp[1] == $arrStart[1] && $arrIp[1] == $arrEnd[1]) {
$arrIp[2] = rand($arrStart[2], $arrEnd[2]);
} elseif ($arrIp[1] == $arrStart[1]) {
$arrIp[2] = rand($arrStart[2], 255);
} elseif ($arrIp[1] == $arrEnd[1]) {
$arrIp[2] = rand(0, $arrEnd[2]);
} else {
$arrIp[2] = rand(0, 255);
}
// Fourth
if ($arrIp[2] == $arrStart[2] && $arrIp[02] == $arrEnd[2]) {
$arrIp[3] = rand($arrStart[3], $arrEnd[3]);
} elseif ($arrIp[2] == $arrStart[2]) {
$arrIp[3] = rand($arrStart[3], 255);
} elseif ($arrIp[2] == $arrEnd[2]) {
$arrIp[3] = rand(0, $arrEnd[3]);
} else {
$arrIp[3] = rand(0, 255);
}
return implode(".", $arrIp);
}