The example of this article describes the approach of PHP for IP address mask operation. Share to everyone for your reference, specific as follows:
IP Resolution:
function Ip_parse ($ip _str) {
$mark _len =;
if (Strpos ($ip _str, "/") > 0) {
list ($ip _str, $mark _len) = Explode ("/", $ip _str);
}
$ip = Ip2long ($ip _str);
$mark = 0xFFFFFFFF << (32-$mark _len) & 0xFFFFFFFF;
$ip _start = $ip & $mark;
$ip _end = $ip | (~ $mark) & 0xFFFFFFFF;
Return Array ($ip, $mark, $ip _start, $ip _end);
Demo:
list ($ip, $mark, $ip _start, $ip _end) = Ip_parse ("192.168.1.12/24");
echo "IP address:", Long2ip ($IP), "\ n";
echo "Subnet mask:", Long2ip ($mark), "\ n";
echo "IP segment start:", Long2ip ($ip _start), "\ n";
echo "IP segment End:", Long2ip ($ip _end), "\ n";
Results:
IP address: 192.168.1.12
subnet Mask: 255.255.255.0
IP segment start: 192.168.1.0
IP segment end: 192.168.1.255
IP is in the IP segment:
function ip_in ($IP, $ip _str) {
$mark _len =;
if (Strpos ($ip _str, "/") > 0) {
list ($ip _str, $mark _len) = Explode ("/", $ip _str);
}
$right _len = 32-$mark _len;
Return Ip2long ($IP) >> $right _len = = Ip2long ($ip _str) >> $right _len
;
Demo:
var_dump (ip_in ("192.168.1.1", "192.168.1.0/24"));
PS: Here again for you to provide an online subnet mask calculation tool for everyone to use for reference:
Online Network Calculator | TCP/IP subnet mask calculation and conversion tool:
Http://tools.jb51.net/aideddesign/ipcalc
More interested in PHP related content readers can view the site topics: "PHP Network Programming Skills Summary", "PHP operation and operator Usage Summary", "PHP file Operation Summary", "Basic PHP Grammar Introduction Tutorial", "PHP operation Office Document skills Summary (including Word, Excel,access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.