Best way to save IPv6 in UNKNOWN DB?

I know that this question has been asked quite a few times, but I could not find a final solution; so here:

How do you store IP (both IPv4 and IPv6), using best practices, in a database without knowing the database? This is the case for database abstraction purposes such as PHP PDO.

In a minor note, I have PHP 5.2.17, since I need PEAR for Windows.

People suggested storing it as varbinary (16) and using the mysql functions inet6_ntop and inet6_pton to pass IP addresses back and forth as strings; e.g. Extending MySQL 5 using IPv6 features . In PHP inet_pton () and inet_ntop () function can convert IPv4 and IPv6 in a binary format as the ThiefMaster in this question suggests , but it is unclear how to transfer binary content in SQL INSERT / UPDATE line (the php-function only available php 5.3.0 on the windows , although you can redesign them). I really like what Jake did and his results regarding integer representations of IP addresses in DBand this may come in handy in some distant, unforeseen future if I have to implement this in my database, but then I’m not sure of the cross-compatibility of the database for abstracting the database using PHP PDO. This post seems to give a close answer on storing binary values, but can't binary injection into potential danger strings be immobilized? Also, if you follow this route, how many DBs can convert varbinary (16) / int (128 bits) to a representative IP if any developer wants to do some quick searches?

It seems to me that the easiest way is to insert ip string as-is into varchar (45) . But how could those who want to follow a difficult route, in PHP (the reverse design like djmaze (AT) dragonflycms (.) Org or like MagicalTux on FF dot st offer) using the functions inet_ntop () and inet_pton (), store and retrieve IPv6 as binary files? Can someone give an example of PDO from <?php $strIP = "2001:4860:b002::68"; ?>using INSERT and SELECT statements?

As you can see, I did my research, but I don’t understand the final good practice of this IPv6.

+3
source share
1 answer

, IP- , . , IP- .

, . , (==, > , <, ..), .

, :

function expand_ip_address($addr_str) {
  /* First convert to binary, which also does syntax checking */
  $addr_bin = @inet_pton($addr_str);
  if ($addr_bin === FALSE) {
    return FALSE;
  }

  /* Then differentiate between IPv4 and IPv6 */
  if (strlen($addr_bin) == 4) {
    /* IPv4: print each byte as 3 digits and add dots between them */
    return implode('.', array_map(
      create_function('$byte', 'return sprintf("%03d", ord($byte));'),
      str_split($addr_bin)
    ));
  } else {
    /* IPv6: print as hex and add colons between each group of 4 hex digits */
    return implode(':', str_split(bin2hex($addr_bin), 4));
  }
}

:

/* Test IPv4 */
var_dump(expand_ip_address('192.0.2.55'));

/* Test IPv6 */
var_dump(expand_ip_address('2001:db8::abc'));

/* Test invalid */
var_dump(expand_ip_address('192:0:2:55'));

:

string(15) "192.000.002.055"
string(39) "2001:0db8:0000:0000:0000:0000:0000:0abc"
bool(false)
+2

All Articles