Thread: IPv6 in PHP

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    55
    Rep Power
    1

    IPv6 in PHP


    How to get IPv6 protocol suported in PHP? What table field i need to have, what functions i need to use? Everytime a member would use ipv6 my script would just crash or not display his proper ip.
    Thank you.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,322
    Rep Power
    630
    Depends on what you are trying to accomplish. The function inet_ntop supports IPv6.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    55
    Rep Power
    1
    Instead of long2ip or ip2long(i see i have them all over the code) i can use inet_ntop and would work for both ipv4 and ipv6 ? and what varchar should i have my ip field in the sql? for the moment i set it at (64) would it be enough to store ipv6? i know that for ipv4 is more than enough
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,322
    Rep Power
    630
    I think this will answer your question.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    55
    Rep Power
    1
    So varchar(64) is more than enough.. Good. Hmm gonna put to the test that function and see if it does the job.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    55
    Rep Power
    1
    $nip = ip2long($ip);
    changing this variable with $nip = inet_pton($ip); wont work. what i am doing wrong?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,322
    Rep Power
    630
    This is hardly a good question since none of us are clairvoyant. What does $ip contain if you print it out? What error message are you getting? What does $nip contain that is different from what you expect?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,335
    Rep Power
    9645
    ip2long() and inet_pton() do two different things. Only one of them can possibly be appropriate to use.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,322
    Rep Power
    630
    I think the OP showed what was changed from and to.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    55
    Rep Power
    1
    $ip contains the ip address of the member.its in varchar(64)
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    55
    Rep Power
    1
    i want to change from ip2long to something else that will allow the site to also support ipv6.
  22. #12
  23. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,080
    Rep Power
    4101
    Unless you have some particular need to convert an IP into binary format you should just leave it as a simple text string and store that into your database. In other words don't bother with either ip2long or inet_pton. Just make sure your database field size is large enough to hold a IPv6 address and your good on that end of things.

    If you code tries to do stuff with the IP in a IPv4 specific way (such as say $quads = explode('.', $ip)) then you'll need to adapt it as well.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo