4

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
Keverw
  • 3,736
  • 7
  • 31
  • 54
  • 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 Answers1

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