Posted By

Hexahow on 11/09/12


Tagged

IPv4 IPv6


Versions (?)

Calculate and Convert IPv6 to Decimal


 / Published in: PHP
 

URL: http://www.ip2location.com/free/ipv6

/********************************************************************************/ /* Description: Calculate and Convert IPV6 to Decimal. / / For information, please visit http://www.ip2location.com / /********************************************************************************/ / You can obtain free IPV6 database at http://www.ip2location.com/free/ipv6 */

  1. <?php
  2.  
  3. function ip62long($ipv6){
  4. $ip_n = inet_pton($ipv6);
  5. $bits = 15;
  6.  
  7. $ipv6long = 0;
  8.  
  9. while($bits >= 0){
  10. $bin = sprintf("%08b",(ord($ip_n[$bits])));
  11.  
  12. if($ipv6long){
  13. $ipv6long = $bin . $ipv6long;
  14. }
  15. else{
  16. $ipv6long = $bin;
  17. }
  18. $bits--;
  19. }
  20. return gmp_strval(gmp_init($ipv6long, 2), 10);
  21. }
  22.  
  23. ?>

Report this snippet  

You need to login to post a comment.