note 14413 added to function.ip2long.php

From: Date: Mon, 30 Jul 2001 15:54:10 +0000
Subject: note 14413 added to function.ip2long.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-16065@lists.php.net to get a copy of this message
If you want to check if an IP is within a specified network you can use the following function: # # IP_match($network, $mask, $ip); # # FUNCTION to check # if an IP is within the specified mask. # function IP_match($network, $mask, $ip) { $ip_long=ip2long($ip); $mask_long=ip2long($network); # # Convert mask to divider # if (is_integer($network)) { /// 212.50.13.0/27 style mask (Cisco style) $divider=2^(32-$mask); } else { /// 212.50.13.0/255.255.255.0 style mask $divider=(2^32-ip2long($mask)); } # # Test is IP within specified mask # if ( floor($ip_long/$divider) == floor($mask_long/$divider)){ # match - this IP is within specified mask return true; } else { # fail - this IP is NOT within specified mask return false; } } http://www.php.net/manual/en/function.ip2long.php

« previous php.notes (#16065) next »