PHP asks a network segment to start and end the IP address method, phpip_php tutorial

Source: Internet
Author: User

PHP asks a network segment to start and end the IP address method, PHPIP


In this paper, we describe a method for PHP to start and end an IP address in a network segment. Share to everyone for your reference. Specific as follows:

For example: Network segment (192168.1.5/24), whose subnet mask is divided according to 24:
11111111.11111111.11111111.00000000 (255.255.255.0)
Description: The IP address is 32bits,24 in the division of the network segment is indicated in front of 24 1, followed by 8 0.
The algorithm for starting the IP address is: The binary of the 192.168.1.5 and the subnet mask binary are "with" the Operation out.
The algorithm for ending an IP address is: The binary of the subnet mask is first reversed, and then the "or" operation is performed with the binary of the 192.168.1.5
The actual application, that is, the network address and broadcast address, network address +1 is the first host address, broadcast address 1 is the last host address.

<?phpfunction Mask2bin ($n) {$n = Intval ($n); if ($n <0| |  $n >32) die (' Error submask '); Return Str_repeat ("1", $n). Str_repeat ("0", 32-$n);}  function Revbin ($s) {$p =array (' 0 ', ' 1 ', ' 2 ');   $r =array (' 2 ', ' 0 ', ' 1 '); Return Str_replace ($p, $r, $s);}  function StartIP ($STR, $bSub) {$bIp = Decbin ($STR);  $bIp = Str_pad ($bIp, 8, "0", str_pad_left);  $sIp = Bindec ($bIp & $bSub); return $sIp;}  function EndIP ($STR, $bSub) {$bIp = Decbin ($STR);  $bIp = Str_pad ($bIp, 8, "0", str_pad_left);  $eIp = Bindec ($bIp | revbin ($bSub)); return $eIp;}          $ip = Array (' 192 ', ' 168 ', ' 1 ', ' 5 ');//Set IP address, can be obtained from the form, here only for demonstration $mask = ' 24 ';     Set Mask $bsub = Mask2bin ($mask);  Convert the subnet mask into binary $mask = Array (), $mask [] = substr ($bSub, "0", 8); Place the subnet mask every 8 bits $mask[] = substr ($bSub, "8", 8), $mask [] = substr ($bSub, "", 8); $mask [] = substr ($bSub, "" ", 8); Echo '
 
 
Mask: ' For ($i =0; $i <4; $i + +) {echo bindec ($mask [$i]); if ($i!=3) echo ".";} Echo '
Network address: '; for ($i =0; $i <4; $i + +) {echo StartIP ($ip [$i], $mask [$i]); if ($i!=3) echo "."; }echo '
The first one is available: '; for ($i =0; $i <3; $i + +) {echo StartIP ($ip [$i], $mask [$i]); echo "."; } $ip _4 = StartIP ($ip [3], $mask [3]); echo + + $ip _4; Echo '
Last available: '; for ($i =0; $i <3; $i + +) {echo EndIP ($ip [$i], $mask [$i]); echo "."; } $ip _4 = EndIP ($ip [3], $mask [3]); echo--$ip _4;echo '
broadcast address: '; for ($i =0; $i <4; $i + +) {echo EndIP ($ip [$i], $mask [$i]); if ($i!=3) echo "."; }?>.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/phpjc/1029594.html www.bkjia.com true Span id= "Isbasedonurl" itemprop= "Isbasedonurl" >http://www.bkjia.com/phpjc/1029594.html techarticle php find a way to start and end IP addresses in a network segment, PHPIP In this paper, we describe a method for PHP to start and end an IP address in a network segment. Share to everyone for your reference. Specific as follows: ...

  • 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.