0x00 Posted November 1, 2015 Share Posted November 1, 2015 (edited) Long story short... echo ":: ".inet_pton("127.0.0.1"); Is printing an empty string...!? The manual says "(if PHP was built with IPv6 support enabled)": http://php.net/manual/en/function.inet-pton.php I've done a phpinfo() and in both initial block and curl block it states IPv6 Support as enabled. So... further looking and I found this page: https://www.mikemackintosh.com/5-tips-for-working-with-ipv6-in-php/ It mentions having to unpack dependently, like so: $ip="127.0.0.1"; $p=""; if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)){ $p= current( unpack( "A4", inet_pton( $ip ) ) ); } elseif(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)){ $p= current( unpack( "A16", inet_pton( $ip ) ) ); } echo ":: ".$p; I'm still getting an empty string... What, where, why, . . how? Cheers EDIT: I've spelt the title wrong! EDIT 2: if (defined('AF_INET6')) { echo "PHP was compiled without --disable-ipv6 option"; } else { echo "PHP was compiled with --disable-ipv6 option"; } ...without Edited November 1, 2015 by 0x00 Quote Link to comment https://forums.phpfreaks.com/topic/298974-inet_pton/ Share on other sites More sharing options...
Jacques1 Posted November 1, 2015 Share Posted November 1, 2015 What exactly are you trying to do? The purpose of inet_pton() is to turn a human-readable IP address into a binary string. So making the string unprintable is pretty much the whole point. Appearently you want some human-readable format, so what would that look like? Quote Link to comment https://forums.phpfreaks.com/topic/298974-inet_pton/#findComment-1525056 Share on other sites More sharing options...
0x00 Posted November 1, 2015 Author Share Posted November 1, 2015 I'm storing in db (currently using INT UNSIGNED) so that I can search for banned ranges. ref for variable type: https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet-aton was originally going to use a LONG: https://dev.mysql.com/doc/refman/5.0/en/other-vendor-data-types.html er, yes, so it outputs 0 no matter what when I was reading the DB (before debugging, i.e. long story short) Quote Link to comment https://forums.phpfreaks.com/topic/298974-inet_pton/#findComment-1525057 Share on other sites More sharing options...
Jacques1 Posted November 1, 2015 Share Posted November 1, 2015 ip2long() Quote Link to comment https://forums.phpfreaks.com/topic/298974-inet_pton/#findComment-1525058 Share on other sites More sharing options...
0x00 Posted November 1, 2015 Author Share Posted November 1, 2015 That only handles ipv4, not ipv6... Quote Link to comment https://forums.phpfreaks.com/topic/298974-inet_pton/#findComment-1525059 Share on other sites More sharing options...
Jacques1 Posted November 1, 2015 Share Posted November 1, 2015 If you want IPv6, then using an integer type makes no sense. You need a VARBINARY(16), and you have to pass the binary return value of inet_pton() directly to your database. Or use the MySQL functions instead of doing the conversion in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/298974-inet_pton/#findComment-1525060 Share on other sites More sharing options...
0x00 Posted November 1, 2015 Author Share Posted November 1, 2015 Thats why I was originally going to use the LONG which maps to MEDIUMTEXT, but it suggested in the manual that it should use INT UNSIGNED (when using internal function). Also I want to do some pre-checks, etc. $n1=null; $n2=null; if(filter_var($posts['ip1'],FILTER_VALIDATE_IP)){ $n1=inet_pton($posts['ip1']); } if(filter_var($posts['ip2'],FILTER_VALIDATE_IP)){ $n2=inet_pton($posts['ip2']); } if($n1!==false&&$n2!==false){ ...DB } It works with either LONG or VARBINARY(16)... Thankyou Quote Link to comment https://forums.phpfreaks.com/topic/298974-inet_pton/#findComment-1525061 Share on other sites More sharing options...
Solution Jacques1 Posted November 1, 2015 Solution Share Posted November 1, 2015 The manual suggests INT UNSIGNED for IPv4. Since IPv4 addresses cover only 32 bits, they fit into an 32-bit integer type. IPv6 addresses cover 128 bits, so they cannot possibly fit into any integer type. Hence my confusion as to what you actually want. It's unclear to me why you'd want to use LONG. This is a special compatibility type for exchanging code between MySQL and a non-MySQL database system. It's also a text type as opposed to a binary type, which means it uses a character encoding and sorts by a character collation. None of this makes sense for purely binary data like a raw IP address. So you'll want the binary type VARBINARY(16). This can store up to 128 bits. 1 Quote Link to comment https://forums.phpfreaks.com/topic/298974-inet_pton/#findComment-1525065 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.