Jump to content

inet_pton


0x00
Go to solution Solved by Jacques1,

Recommended Posts

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 by 0x00
Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Solution

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.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.