What is the correct way to save an IP in the database?

Asked

Viewed 519 times

1

How to save both IP types IPV4 and IPV6 in a Mysql database? What is the correct typing and column size? Any other remarks?

I receive the IP through the $_SERVER['REMOTE_ADDR'] with PHP.

1 answer

2

There are functions to use in IPV4.

For Ipv4 addresses, you may want to store them as a int unsigned and use the functions INET_ATON() and INET_NTOA() to return the IP address of its numeric value and vice versa.

https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet-aton

https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet-ntoa

To Ipv6:

Use that kind VARBINARY(16)

https://dev.mysql.com/doc/refman/5.6/en/miscellaneous-functions.html#function_inet6-aton

Browser other questions tagged

You are not signed in. Login or sign up in order to post.