note 99251 added to function.ip2long
| From: | oni-kun | Date: | Fri, 06 Aug 2010 05:34:40 +0000 |
| Subject: | note 99251 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171417@lists.php.net to get a copy of this message | ||
For purely practise (and a better understanding of bytes), you could write an efficient alternative
using PHP's bitshift operators to handle the division/multiplication/powers:
<?php
function _ip2long($arg) {
$numbers = explode (".", $arg);
return ( $numbers[0] << 24) +
( $numbers[1] << 16) +
( $numbers[2] << 8 ) +
( $numbers[3] );
}
function _long2ip($arg) {
return ($arg >> 24 & 255) . "." .
($arg >> 16 & 255) . "." .
($arg >> 8 & 255) . "." .
($arg & 255);
}
//2130706433
print $ip = _ip2long($_SERVER['REMOTE_ADDR']) . "<br/>";
//127.0.0.1
print _long2ip($ip);
?>
----
Server IP: 66.207.199.35
Probable Submitter: 174.1.235.8
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- https://master.php.net/note/edit/99251
Del: integrated -- https://master.php.net/note/delete/99251/integrated
Del: useless -- https://master.php.net/note/delete/99251/useless
Del: bad code -- https://master.php.net/note/delete/99251/bad+code
Del: spam -- https://master.php.net/note/delete/99251/spam
Del: non-english -- https://master.php.net/note/delete/99251/non-english
Del: in docs -- https://master.php.net/note/delete/99251/in+docs
Del: other reasons-- https://master.php.net/note/delete/99251
Reject -- https://master.php.net/note/reject/99251
Search -- https://master.php.net/manage/user-notes.php