When storing a client's IP in long
form (using PHP's ip2long
) how large does the database field have to be?
Asked
Active
Viewed 162 times
0

Jordan Doyle
- 2,976
- 4
- 22
- 38
2 Answers
2
4 bytes (assuming IPv4). That is all that is required.
EDIT
If you want to store IPv8 you need 16 bytes. You can use the mechanism as described on http://en.wikipedia.org/wiki/IPv6 to differentiate between the two

Ed Heal
- 59,252
- 17
- 87
- 127
-
Doesn't `ip2long` only work on IPv4 addresses? I'm looking at the PHP manual and it seems to have an example of `ip2long` returning `3221234342`, that is 10 bytes, isn't it? – Jordan Doyle Sep 15 '13 at 10:22
-
@JordanDoyle - That is why I added "(assuming IPv4)" – Ed Heal Sep 15 '13 at 10:31
1
15 characters for IPv4 (xxx.xxx.xxx.xxx format, 12+3 separators)
39 characters (32 + 7 separators) for IPv6
8 groups of 4 digits with 7 ':' between them
So that is
(8*4)+7=39
or If consider IPv4
[0000:0000:0000:0000:0000:0000:192.168.0.1]
then
(6 * 4 + 5) + 1 + (4 * 3 + 3) = 29 + 1 + 15 = 45
Or as jordan said You can also store the IP addr like this
INSERT table(ip) VALUES (INET_ATON('192.168.0.1')); /*ip = 3232235521*/
SELECT INET_NTOA(ip) As IPAddress FROM table;

Vaibs_Cool
- 6,126
- 5
- 28
- 61
-
He's converting the IP address to an int using `ip2long()`. http://php.net/manual/en/function.ip2long.php – Annika Backstrom Sep 15 '13 at 10:23
-
What about when the IP is in `long` form such as `3221234342`? EDIT: Looks like Adam beat me to it – Jordan Doyle Sep 15 '13 at 10:23
-