I have a php site. I want to store an last ip for each user. I want it to work for both IPV4 and IPV6 users. Any idea how?
Asked
Active
Viewed 2,067 times
4
-
Where is your problem? Connecting to a database in php, DB design, getting users IP,...? – hage Mar 20 '11 at 19:28
-
Dup of: http://stackoverflow.com/questions/420680/how-to-store-ipv6-compatible-address-in-a-relational-database Note the accepted answer, it explains how you would differentiate between IPv4 and IPv6 – Brian Roach Mar 20 '11 at 19:29
1 Answers
6
Simply convert them to a binary representation using inet_pton()
and later back to a string using inet_ntop()
.
Another solution would be storing the IP string without any transformations in the DB. A VARCHAR(39)
field will do the job as the highest/longest IPv6 is ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff

ThiefMaster
- 310,957
- 84
- 592
- 636