note 70055 added to function.ip2long

From: Date: Sun, 01 Oct 2006 20:40:55 +0000
Subject: note 70055 added to function.ip2long
Groups: php.notes 
Request: Send a blank email to php-notes+get-117910@lists.php.net to get a copy of this message
I propose a new function to match an IP against a network. You can define the network in two different manner, most suited for handling data grabed from WHOIS records: - 202.1.192.0-202.1.192.255: a range of IPs - 200.36.161.0/24: a range of IP by using net masking - 200.36.161/24: a shorten syntax similar to the above. Sdl <?php function netMatch($network, $ip) { $network=trim($network); $ip = trim($ip); $d = strpos($network,"-"); if ($d===false) { $ip_arr = explode('/', $network); if (!preg_match("@\d*\.\d*\.\d*\.\d*@",$ip_arr[0],$matches)){ $ip_arr[0].=".0"; // Alternate form 194.1.4/24 } $network_long = ip2long($ip_arr[0]); $x = ip2long($ip_arr[1]); $mask = long2ip($x) == $ip_arr[1] ? $x : (0xffffffff << (32 - $ip_arr[1])); $ip_long = ip2long($ip); return ($ip_long & $mask) == ($network_long & $mask); } else { $from = trim(ip2long(substr($network,0,$d))); $to = trim(ip2long(substr($network,$d+1))); $ip = ip2long($ip); return ($ip>=$from and $ip<=$to); } } ?> ---- Server IP: 66.163.161.117 Probable Submitter: 90.11.58.80 ---- Manual Page -- http://www.php.net/manual/en/function.ip2long.php Edit -- https://master.php.net/note/edit/70055 Del: integrated -- https://master.php.net/note/delete/70055/integrated Del: useless -- https://master.php.net/note/delete/70055/useless Del: bad code -- https://master.php.net/note/delete/70055/bad+code Del: spam -- https://master.php.net/note/delete/70055/spam Del: non-english -- https://master.php.net/note/delete/70055/non-english Del: in docs -- https://master.php.net/note/delete/70055/in+docs Del: other reasons-- https://master.php.net/note/delete/70055 Reject -- https://master.php.net/note/reject/70055 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#117910) next »