php输出随机IP的方法
墨初 编程开发 959阅读
因为某些需求,需要利用php脚本生成一个随机的ip地址。下面是列举出几种利用php脚本生成随机IP地方法。
php几个生成随机的IP地址的方法
方法1:
先设置几组IP地址选择的范围,然后随机从几组IP选择范围内返回IP地址。
$ip_long = array( array('607649792', '608174079'), //36.56.0.0-36.63.255.255 array('1038614528', '1039007743'), //61.232.0.0-61.237.255.255 array('1783627776', '1784676351'), //106.80.0.0-106.95.255.255 array('2035023872', '2035154943'), //121.76.0.0-121.77.255.255 array('2078801920', '2079064063'), //123.232.0.0-123.235.255.255 array('-1950089216', '-1948778497'), //139.196.0.0-139.215.255.255 array('-1425539072', '-1425014785'), //171.8.0.0-171.15.255.255 array('-1236271104', '-1235419137'), //182.80.0.0-182.92.255.255 array('-770113536', '-768606209'), //210.25.0.0-210.47.255.255 array('-569376768', '-564133889'), //222.16.0.0-222.95.255.255 ); $rand_key = mt_rand(0, 9); $ip = long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1])); echo $ip; // 123.233.97.116
方法2:
php通过随机IP的每个子段取值,来随机组合出一个新的IP地址 //自定义IP的开头 $arr_1 = array("218","218","66","66","218","218","60","60","202","204","66","66","66","59","61","60","222","221","66","59","60","60","66","218","218","62","63","64","66","66","122","211"); $randarr= mt_rand(0,count($arr_1)-1); $ip1id = $arr_1[$randarr]; //下面是自定义IP的后三段 $ip2id= round(rand(600000, 2550000) / 10000); $ip3id= round(rand(600000, 2550000) / 10000); $ip4id= round(rand(600000, 2550000) / 10000); echo $ip1id.'.'.$ip2id.'.'.$ip3id.'.'.$ip4id; // 61.62.100.211
方法3:
php 可以随机一组定义个的IP地址,进行取值。这种方法取值的范围比较小。 $ip_arrs = array( '192.168.34.1', '225.15.56.245', '52.245.54.263', '57.58.68.45', '5.8.54.2', '114.114.115.115', '8.8.8.8' ); echo $ip_arrs[array_rand($ip_arrs)];