Calculate and Convert IPv6 to Decimal


/ Published in: PHP
Save to your folder(s)

/********************************************************************************/
/* 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 */


Copy this code and paste it in your HTML
  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. ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.