note 90344 added to function.ip2long
| From: | spinyn at gmail dot com | Date: | Fri, 17 Apr 2009 02:51:00 +0000 |
| Subject: | note 90344 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-153419@lists.php.net to get a copy of this message | ||
Just want to add a comment to kaputt's valuable contribution to the task of matching hosts to
ip ranges, efficiently. The script works fine if the binary representation of the ip involves no
leading zeros. Unfortunately, the way decbin() seems to work, leading zeros in the binary
representation of the first ip quad get dropped. That is a serious matter if you're trying to
match all possible candidates in the checklist. In those cases the leading zeros need to be added
back to get accurate matches for values in the first quad between 0-127 (or the binary equivalent,
0-01111111).
The solution I came up with to address this issue was the following function:
function addLeadingZero($ip) {
if (($result = (32 - strlen($ip))) > 0)
return str_repeat("0", $result).$ip;
}
----
Server IP: 208.69.120.35
Probable Submitter: 69.72.26.113
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- https://master.php.net/note/edit/90344
Del: integrated -- https://master.php.net/note/delete/90344/integrated
Del: useless -- https://master.php.net/note/delete/90344/useless
Del: bad code -- https://master.php.net/note/delete/90344/bad+code
Del: spam -- https://master.php.net/note/delete/90344/spam
Del: non-english -- https://master.php.net/note/delete/90344/non-english
Del: in docs -- https://master.php.net/note/delete/90344/in+docs
Del: other reasons-- https://master.php.net/note/delete/90344
Reject -- https://master.php.net/note/reject/90344
Search -- https://master.php.net/manage/user-notes.php