PHP functions for randomly generating domestic IP addresses-PHP source code

Source: Internet
Author: User
To generate an IP address, we only need to generate a string. The ipv4 ip address data can be generated in the int storage space within the specified range. Let's take a look. to generate an IP address, we only need to generate a string. The ipv4 ip address data can be generated in the int storage space within the specified range. Let's take a look.

Script ec (2); script

Sometimes it is necessary to forge an IP address to capture other website content, preferably a domestic IP address. I found a function on the Internet and can randomly find the domestic IP addresses of 10 IP segments. I checked some IP address data myself, you have manually added 5 IP segments and a total of 15 IP segments, so that you can randomly add many domestic IP addresses, with code:

Function rand_ip (){
$ Ip_long = array (
Array ('100', '100'), // 36.56.0.0-36.63.255.255
Array ('100', '100'), // 58.30.0.0-58.63.255.255
Array ('20140901', '20140901'), // 59.151.0.0-59.151.127.255
Array ('20140901', '20140901'), // 60.194.0.0-60.195.20.255
Array ('20140901', '20140901'), // 61.232.0.0-61.237.255.255
Array ('20140901', '20140901'), // 106.80.0.0-106.95.20.255
Array ('20140901', '20140901'), // 116.13.0.0-116.13.255.255
Array ('20140901', '20140901'), // 118.112.0.0-118.126.255.255
Array ('20140901', '20140901'), // 121.76.0.0-121.77.255.255
Array ('20140901', '20140901'), // 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.20.255
Array ('-770113536', '-768606209'), // 210.25.0.0-210.47.255.255
Array ('-569376768', '-564133889'), // 222.16.0.0-222.95.20.255
);
$ Rand_key = mt_rand (0, 14 );
$ Huoduan_ip = long2ip (mt_rand ($ ip_long [$ rand_key] [0], $ ip_long [$ rand_key] [1]);
Return $ huoduan_ip;
}

Supplement: long2ip Function

The storage space is reduced to nearly 1/4 (15 bytes of char (15) to 4 bytes of the integer), and it is easier to calculate whether a specific address is in the page of a specific segment, it also speeds up searching and sorting (although sometimes only a little faster ).

Example

$ Ip = long2ip (3232235881 );
Echo $ ip; // 127.20.255

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.