views:

4547

answers:

2

I want to store the data returned by $_SERVER["REMOTE_ADDR"] in PHP into a DB field, pretty simple task, really. The problem is that I can't find any proper information about the maximum length of the textual representation of an IPv6 address, which is what a webserver provides through $_SERVER["REMOTE_ADDR"].

I'm not interested in converting the textual representation into the 128 bits the address is usually encoded in, I just want to know how many characters maximum are needed to store any IPv6 address returned by $_SERVER["REMOTE_ADDR"].

+6  A: 

Answered my own question:

IPv6 addresses are normally written as eight groups of four hexadecimal digits, where each group is separated by a colon (:).

So that's 39 characters max.

Gilles
+18  A: 

I believe it would be:

8 * 4 + 7 = 39

8 groups of 4 digits with 7 ':' between them.

Or, if you want to take into account the IPv4 tunneling features [0000:0000:0000:0000:0000:0000:192.168.0.1],

(6 * 4 + 5) + 1 + (4 * 3 + 3) = 29 + 1 + 15 = 45

Matthew Scharley
I'm in a generous mood, but I did post my answer first. I left the question open in case someone would contradict my finding.
Gilles
0000:0000:0000:0000:0000:0000:127.127.127.127As I read here: http://tools.ietf.org/html/rfc4291, this could be valid. And this is 45 chars.
Vili
Allow me to be ther first to say: That's just a bastardised IPv4 address. But you are right.
Matthew Scharley
Header files define INET6_ADDRSTRLEN to be 46, which fits with 45 chars plus a trailing null.
wisnij