What is the best field type and length for storing IP addresses in a MySQL database?
What about for IPv6?
We can store an IP address with the help of INT unsigned. While using INSERT, include INET_ATON() and with SELECT, include INET_NTOA(). IP address is in dotted format.
IP Network Address Data Types. IPV4 and IPV6 are abstract data types that store IPv4 and IPv6 host addresses, respectively, in binary format. IPV4 is a 4-byte host address in dotted-decimal notation (four decimal numbers, each ranging from 0 to 255, separated by dots). For example: 172.16.
There is nothing wrong with storing the IP address of a visitor. There are any number of valid reasons to store IP addresses of visitors to your website (security, analytics, etc). Nothing, except the privacy law in many European countries.
Geolocation IP Databases allow you to determine your website visitor's location. These databases of IP addresses contain the latitude and longitude of a particular IP address. So when you look up an IP address, you are provided with the pair of coordinates that are the 'origin' of that IP address.
Store the ip as a INT(11) UNSIGNED
, then use the INET_ATON
and INET_NTOA
functions to store/retrieve the ip address.
Sample code:
INSERT table(ip) VALUES (INET_ATON('192.168.0.1')); /*ip = 3232235521*/ SELECT INET_NTOA(ip) As IPAddress FROM table; /*IPAddress = 192.168.0.1*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With