Store IPv6 in database

前端 未结 6 863
无人及你
无人及你 2020-12-08 22:12

What\'s the best practise to store IP\'s with PHP in MySQL database? There\'s a function called ip2long - but this is just for IPv4. But what about IPv6?

I know a ph

6条回答
  •  青春惊慌失措
    2020-12-08 23:00

    knittl was closer, instead of binary(16) use varbinary(16) as user196009 answered in a related question. It works for me. How?

    Storing IP:

    getResults($query); // stored as binary
    ?>
    

    Retrieving IP:

    getRow($query);
      echo inet_ntop($stats->vis_ip); // outputs 66.102.7.104
    ?> 
    

    It should work with IPv6 addresses (I have an IPv4 connection). I'm not an expert so I don't know yet if varbinary length is correct, but how I said, it works for me.

    In order to check if 'IPv6 Support' is enabled in your PHP version/host:

     
    

提交回复
热议问题