Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store IP in database?

Tags:

php

mysql

What is the best field type and length for storing IP addresses in a MySQL database?

What about for IPv6?

like image 625
Mike Avatar asked Feb 13 '11 05:02

Mike


People also ask

Can I store IP address in database?

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.

What data type is an IP address?

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.

Is it legal to store user IP?

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.

What are IP databases?

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.


1 Answers

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*/ 
like image 137
The Scrum Meister Avatar answered Oct 02 '22 00:10

The Scrum Meister