note 71939 added to function.ip2long
| From: | IanB at osu1 dot php dot net | Date: | Sun, 24 Dec 2006 12:22:24 +0000 |
| Subject: | note 71939 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120887@lists.php.net to get a copy of this message | ||
NOTE: ip2long() should NOT be used for CIDR calculation.
Instead, you should use something like the following:
<?php
/* get the base and the bits from the ban in the database */
list($base, $bits) = explode('/', $CIDR);
/* now split it up into it's classes */
list($a, $b, $c, $d) = explode('.', $base);
/* now do some bit shfiting/switching to convert to ints */
$i = ($a << 24) + ($b << 16) + ($c << 8) + $d;
$mask = $bits == 0 ? 0 : (~0 << (32 - $bits));
/* here's our lowest int */
$low = $i & $mask;
/* here's our highest int */
$high = $i | (~$mask & 0xFFFFFFFF);
/* now split the ip were checking against up into classes */
list($a, $b, $c, $d) = explode('.', $iptocheck);
/* now convert the ip we're checking against to an int */
$check = ($a << 24) + ($b << 16) + ($c << 8) + $d;
/* if the ip is within the range, including
highest/lowest values, then it's witin the CIDR range */
if ($check >= $low && $check <= $high)
return 1;
else
return 0;
?>
This means that you should check to see if the IP
address is of the correct format each time.
----
Server IP: 85.116.0.130
Probable Submitter: 86.1.184.177
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- https://master.php.net/note/edit/71939
Del: integrated -- https://master.php.net/note/delete/71939/integrated
Del: useless -- https://master.php.net/note/delete/71939/useless
Del: bad code -- https://master.php.net/note/delete/71939/bad+code
Del: spam -- https://master.php.net/note/delete/71939/spam
Del: non-english -- https://master.php.net/note/delete/71939/non-english
Del: in docs -- https://master.php.net/note/delete/71939/in+docs
Del: other reasons-- https://master.php.net/note/delete/71939
Reject -- https://master.php.net/note/reject/71939
Search -- https://master.php.net/manage/user-notes.php