note 70707 added to function.ip2long

From: Date: Tue, 24 Oct 2006 16:59:26 +0000
Subject: note 70707 added to function.ip2long
Groups: php.notes 
Request: Send a blank email to php-notes+get-119078@lists.php.net to get a copy of this message
I made one tiny change to Stephane's routine below when I had problems with spaces in an IP range. I moved the trim function before the ip2long call. Thanks Stephane! Dirk. <?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 = ip2long(trim(substr($network,0,$d))); $to = ip2long(trim(substr($network,$d+1))); $ip = ip2long($ip); return ($ip>=$from and $ip<=$to); } } ?> ---- Server IP: 216.194.113.175 Probable Submitter: 24.8.197.191 ---- Manual Page -- http://www.php.net/manual/en/function.ip2long.php Edit -- https://master.php.net/note/edit/70707 Del: integrated -- https://master.php.net/note/delete/70707/integrated Del: useless -- https://master.php.net/note/delete/70707/useless Del: bad code -- https://master.php.net/note/delete/70707/bad+code Del: spam -- https://master.php.net/note/delete/70707/spam Del: non-english -- https://master.php.net/note/delete/70707/non-english Del: in docs -- https://master.php.net/note/delete/70707/in+docs Del: other reasons-- https://master.php.net/note/delete/70707 Reject -- https://master.php.net/note/reject/70707 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119078) next »