[Learning little bit-PHP] encapsulation of common functions of PHP IPv4

Source: Internet
Author: User
<? PHP/*** @ Author: Xiaoqing. huo * @ Date: 2012-12-05 * @ Param: $ IP ==> IPv4 address, $ netbit ==> mask bits */class ipconfigure {private $ IP; private $ netbit; public Function _ construct ($ IP, $ netbit) {$ this-> IP = $ IP; If (! ($ Netbit> = 0 & $ netbit <= 32) {$ this-> netbit = 32 ;}$ this-> netbit = $ netbit ;} // obtain the network number public function getsubnet () {return (long2ip (ip2long ($ this-> ip )) & (ip2long ($ this-> getmask ();} // obtain the IP address public function getip () {return $ this-> IP ;} // obtain the public function getnetbit () {return $ this-> netbit;} // obtain the public function getmask () {$ maskbinstr = str_repeat ("1 ", $ this-> netbit ). str_repeat ("0", 32-$ this-> netbit ); $ Masklong = bindec ($ maskbinstr); Return long2ip ($ masklong);} // gets the available network segment and returns the public function getiprange () of the start and end IP addresses () {$ maskbinstr = str_repeat ("1", $ this-> netbit ). str_repeat ("0", 32-$ this-> netbit); // net mask binary string $ inversemaskbinstr = str_repeat ("0", $ this-> netbit ). str_repeat ("1", 32-$ this-> netbit); // inverse mask $ iplong = ip2long ($ this-> ip); $ ipmasklong = bindec ($ maskbinstr ); $ inversei Pmasklong = bindec ($ inversemaskbinstr); $ network = $ iplong & $ ipmasklong; $ start = $ Network + 1; // remove the network number, host address $ end = ($ Network | $ inverseipmasklong)-1; // remove the broadcast address, return array (long2ip ($ start), long2ip ($ end);} // obtain the broadcast address public function broadcast () {return (long2ip (ip2long ($ this-> getsubnet () | (~ (Ip2long ($ this-> getmask () ;}}$ configure = new ipconfigure ("10.31.144.0", 22); echo "network number :", $ configure-> getsubnet (). php_eol; echo "<PRE>"; echo "available network segment:"; print_r ($ configure-> getiprange (); echo "subnet mask :", $ configure-> getmask (). php_eol; echo "broadcast address:", $ configure-> broadcast (). php_eol;

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.