long2ip() - php 网络函数
long2ip()
(PHP 4, PHP 5, PHP 7)
将长整型转化为字符串形式带点的互联网标准格式地址(IPV4)
说明
long2ip(int $proper_address): stringlong2ip()函数通过长整型的表达形式转化生成带点格式的互联网地址(例如:aaa.bbb.ccc.ddd )。
参数
$proper_address合格的地址,长整型的表达形式。
返回值
返回字符串的互联网 IP 地址。
更新日志
版本 | 说明 |
---|---|
7.1.0 | 参数$proper_address的类型从string改成integer。 |
注释
Note:在 32 位架构中,从string转换integer整型形式的 ip 地址将有可能导致错误的结果,因为结果数字超出了PHP_INT_MAX
限制。
参见
ip2long()
将 IPV4 的字符串互联网协议转换成长整型数字
If the function doesn't exist:
Use these two functions to convert from and to numbers compatible to MySQLs INET_ATON and INET_NTOA
I wanted to be able to pass an IP address in a URL always as an unsigned int. I then converted it back as shown below: $ip_addr = "192.168.100.25"; // example IP address that converts to neg # $s32int = ip2long($ip_addr); $us32str = sprintf("%u",$s32int); // convert to unsigned string // display orig IP address, signed 32 bit version, unsigned 32 bit ver, // finally converted back to IP addr printf("%s ,%d, %s, %s", $ip_addr, $s32int, $us32str, long2ip(-(4294967296-$us32str))); // tested on Linux/Apache PHP 4.1.2
If you're running a 32 bit machine you can get wrong IPs. To prevent from this just cast to float e.g.
For a 32bit safe long2ip, which can accept string or signed integer input, try: function safelong2ip($long) { $binStr = sprintf("%032s", decbin((float)$long)); if (strlen($binStr) != 32) { throw new Exception("Invalid IPv4 subnet!"); } $ipArr = []; for ($i = 0; $iwhen importing ip-ranges to a mysql database using an INT(10) field - NOTE: that you will get problems when using intval() function! copied from "cleong" : 02-Oct-2001 02:21 intval() handles overflow differently depending on the type of the argument.Beware when processing values that are invalid, you may get values that are different based on the OS. For instance: $ip = long2ip(pow(2,32)+1024); On windows you get 255.255.255.255. On linux it's 0.0.4.0. So it seems it would be important to make sure the long you're converting is in the correct range.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)