1
Nội dung chính
- Your Answer
- Sign up or log in
- Post as a guest
- Not the answer you're looking for? Browse other questions tagged phpipipv6 or ask your own question. [adsbygoogle = window.adsbygoogle || []].push[{}];
- 更新日志
To generate a random IPv4 address that's between a certain range, I do this:
$ip = long2ip[rand["16777215", "3758096384"]];
echo $ip;
However, long2ip
doesn't appear to work with IPv6 addresses.
Is there anything similar to generate a random IPv6 address that's between a certain range?
- php
- ip
- ipv6
asked Jan 24, 2017 at 20:51
AndrewAndrew
5652 gold badges6 silver badges11 bronze badges
1
This website appears to be able to convert decimals to IPv6 address, just not sure how they do it... webdnstools.com/dnstools/ipcalc
– Andrew
Jan 25, 2017 at 1:13
Add a comment |
1 Answer
Sorted by: Reset to default
0
//php.net/manual/en/function.long2ip.php : "Converts an long integer address into a string in [IPv4] Internet standard dotted format"
Read this answer: //stackoverflow.com/a/35208011/4067232
edited May 23, 2017 at 12:16
CommunityBot
11 silver badge
answered Jan 26, 2017 at 12:52
Pedro Amaral CoutoPedro Amaral Couto
1,88512 silver badges14 bronze badges
Add a comment |
Your Answer
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Name
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged phpipipv6 or ask your own question. [adsbygoogle = window.adsbygoogle || []].push[{}];
[PHP 4, PHP 5, PHP 7, PHP 8]
ip2long — 将 IPV4 的字符串互联网协议转换成长整型数字
返回值
返回IP地址转换后的数字 或 false
如果 ip_address
是无效的。
更新日志
5.5.0 | Prior to this version, on Windows ip2long[] would sometimes return a valid number even if passed a value which was not an [IPv4] Internet Protocol dotted address. |
5.2.10 | 再此之前的版本, ip2long[] 有时会返回即使这不是一个IPV4的标准地址的数字地址。 |
范例
示例 #1 ip2long[] 例子
示例 #2 显示IP地址
第二个例子说明打印一个转换后的地址使用 printf[] 在PHP4和PHP5的功能:
注释
注意:
因为PHP的 integer 类型是有符号,并且有许多的IP地址讲导致在32位系统的情况下为负数, 你需要使用 "%u" 进行转换通过 sprintf[] 或 printf[] 得到的字符串来表示无符号的IP地址。
注意:
ip2long[] 将返回
false
在IP是255.255.255.255
的情况,版本为 PHP 5
Mike B ¶
4 years ago
spinyn at gmail dot com ¶
13 years ago
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:
php dot net at kenman dot net ¶
14 years ago
To nate, who advises that there is no reason to use an unsigned version of the IP in a MySQL DB:
I think it would depend on your application, but personally, I find it useful to store IP's as unsigneds since MySQL has 2 native functions, INET_ATON[] and INET_NTOA[], which work the same as ip2long[]/long2ip[] _except_ that they generate the unsigned counterpart. So if you want, you could do:
-- IANA Class-B reserved/private
SELECT * FROM `servers`
WHERE `ip` >= INET_ATON['192.168.0.0']
AND `ip`
Ian B ¶
15 years ago
NOTE: ip2long[] should NOT be used for CIDR calculation.
Instead, you should use something like the following: