[ip] Maximum length of the textual representation of an IPv6 address?

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"].

This question is related to ip ip-address ipv6

The answer is


I think @Deepak answer in this link is more close to correct answer. Max length for client ip address. So correct size is 45 not 39. Sometimes we try to scrounge in fields size but it seems to better if we prepare enough storage size.


As indicated a standard ipv6 address is at most 45 chars, but an ipv6 address can also include an ending % followed by a "scope" or "zone" string, which has no fixed length but is generally a small positive integer or a network interface name, so in reality it can be bigger than 45 characters. Network interface names are typically "eth0", "eth1", "wlan0", so choosing 50 as the limit is likely good enough.


Watch out for certain headers such as HTTP_X_FORWARDED_FOR that appear to contain a single IP address. They may actually contain multiple addresses (a chain of proxies I assume).

They will appear to be comma delimited - and can be a lot longer than 45 characters total - so check before storing in DB.


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.


On Linux, see constant INET6_ADDRSTRLEN (include <arpa/inet.h>, see man inet_ntop). On my system (header "in.h"):

#define INET6_ADDRSTRLEN 46

The last character is for terminating NULL, as I belive, so the max length is 45, as other answers.


Examples related to ip

Can't access 127.0.0.1 Correct way of getting Client's IP Addresses from http.Request How to get the IP address of the docker host from inside a docker container How to access site through IP address when website is on a shared host? How to change proxy settings in Android (especially in Chrome) What is the difference between 0.0.0.0, 127.0.0.1 and localhost? socket.error:[errno 99] cannot assign requested address and namespace in python Get client IP address via third party web service Getting IP address of client IIS - can't access page by ip address instead of localhost

Examples related to ip-address

how to get the ipaddress of a virtual box running on local machine How to get ip address of a server on Centos 7 in bash How to extract IP Address in Spring MVC Controller get call? Can You Get A Users Local LAN IP Address Via JavaScript? Get the client IP address using PHP Express.js: how to get remote client address Identifying country by IP address Which terminal command to get just IP address and nothing else? How to find Port number of IP address? C# IPAddress from string

Examples related to ipv6

What is IPV6 for localhost and 0.0.0.0? Make docker use IPv4 for port binding How to set java.net.preferIPv4Stack=true at runtime? what does "dead beef" mean? How can I convert IPV6 address to IPV4 address? Get IPv4 addresses from Dns.GetHostEntry() Is there a way for non-root processes to bind to "privileged" ports on Linux? How do ports work with IPv6? Maximum length of the textual representation of an IPv6 address? Regular expression that matches valid IPv6 addresses