note 85425 added to function.ip2long
| From: | ncrittenatgmaildotyouknowwhat at osu1 dot php dot net | Date: | Fri, 29 Aug 2008 11:58:39 +0000 |
| Subject: | note 85425 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143979@lists.php.net to get a copy of this message | ||
Hi,
Please find below a little function I wrote for handling IP addresses as unsigned integers.
This function will take an IP address or IP number in ANY FORMAT (that I can think of) and will
return it's decimal unsigned equivalent, as a string.
<?php
function myip2long($ip){
if (is_numeric($ip)){
return sprintf("%u", floatval($ip));
} else {
return sprintf("%u", floatval(ip2long($ip)));
}
}
?>
Here is is in action:
<?php
$ip['DottedQuadDec'] = "192.168.255.109";
$ip['PosIntStr'] = "3232300909";
$ip['NegIntStr'] = "-1062666387";
$ip['PosInt'] = 3232300909;
$ip['NegInt'] = -1062666387;
$ip['DottedQuadHex'] = "0xc0.0xA8.0xFF.0x6D";
$ip['DottedQuadOct'] = "0300.0250.0377.0155";
$ip['HexIntStr'] = "0xC0A8FF6D";
$ip['HexInt'] = 0xC0A8FF6D;
printf("\n% 16s : % 20s => % 11s => % 16s","Input Type","Input
Value","Converted","Converted Back");
echo "\n ------------------------------------------------------------------";
foreach ($ip as $type => $value){
$ipnum = myip2long($value);
printf("\n% 16s : % 20s => % 11s => % 16s",$type,$value,$ipnum,long2ip($ipnum));
}
?>
----
Server IP: 92.48.100.16
Probable Submitter: 87.127.73.49
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- https://master.php.net/note/edit/85425
Del: integrated -- https://master.php.net/note/delete/85425/integrated
Del: useless -- https://master.php.net/note/delete/85425/useless
Del: bad code -- https://master.php.net/note/delete/85425/bad+code
Del: spam -- https://master.php.net/note/delete/85425/spam
Del: non-english -- https://master.php.net/note/delete/85425/non-english
Del: in docs -- https://master.php.net/note/delete/85425/in+docs
Del: other reasons-- https://master.php.net/note/delete/85425
Reject -- https://master.php.net/note/reject/85425
Search -- https://master.php.net/manage/user-notes.php