Advertisement
Guest User

qq

a guest
Jun 4th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.54 KB | None | 0 0
  1. <?php
  2. /*
  3. BASE DE DATOS
  4. Una tabla normal pero solo tiene los campos IP1 y IP2 como BIGINT, y IP2 tiene NULL por si no se le quiere pasar nada como en el caso de las IP4
  5.  
  6.  
  7. CREATE TABLE IF NOT EXISTS `accesos` (
  8.   `ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
  9.   `CONEXION` int(11) unsigned NOT NULL,
  10.   `ULTIMA_ACTUALIZACION` int(11) unsigned NOT NULL,
  11.   `ID_USUARIO` int(10) unsigned NOT NULL,
  12.   `IP1` bigint(20) unsigned NOT NULL,
  13.   `IP2` bigint(20) unsigned DEFAULT NULL,
  14.   PRIMARY KEY (`ID`)
  15. ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
  16.  
  17.  
  18.  */
  19.  
  20.  
  21.  
  22.  
  23. // Convierte una ip4 o ip6 a binario
  24. function ip2bin($ip){
  25.     if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false)
  26.         return base_convert(ip2long($ip),10,2);
  27.     if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false)
  28.         return false;
  29.     if(($ip_n = inet_pton($ip)) === false) return false;
  30.     $bits = 15; // 16 x 8 bit = 128bit (ipv6)
  31.     while ($bits >= 0){
  32.         $bin = sprintf("%08b",(ord($ip_n[$bits])));
  33.         $ipbin = $bin.$ipbin;
  34.         $bits--;
  35.     }
  36.     return $ipbin;
  37. }
  38.  
  39. // Convierte un binario de ip4 o ip6 a su presentacion estandar
  40. function bin2ip($bin){
  41.    if(strlen($bin) <= 32) // 32bits (ipv4)
  42.        return long2ip(base_convert($bin,2,10));
  43.    if(strlen($bin) != 128)
  44.        return false;
  45.    $pad = 128 - strlen($bin);
  46.    for ($i = 1; $i <= $pad; $i++)
  47.    {
  48.        $bin = "0".$bin;
  49.    }
  50.    $bits = 0;
  51.    while ($bits <= 7)
  52.    {
  53.        $bin_part = substr($bin,($bits*16),16);
  54.        $ipv6 .= dechex(bindec($bin_part)).":";
  55.        $bits++;
  56.    }
  57.    return inet_ntop(inet_pton(substr($ipv6,0,-1)));
  58. }
  59.  
  60. // Convierte de binario a decimal sin el E+
  61. function BCBin2Dec($Input='') {
  62.   $Output='0';
  63.   if(preg_match("/^[01]+$/",$Input)) {
  64.     for($i=0;$i<strlen($Input);$i++)
  65.       $Output=BCAdd(BCMul($Output,'2'),$Input{$i});
  66.   }
  67.   return($Output);
  68. }
  69.  
  70.  
  71. // Convertimos de nuevo a binario desde el decimal dado con la funcion de arriba
  72. function BCDec2Bin($Input='') {
  73.  $Output='';
  74.  if(preg_match("/^\d+$/",$Input)) {
  75.    while($Input!='0') {
  76.      $Output.=chr(48+($Input{strlen($Input)-1}%2));
  77.      $Input=BCDiv($Input,'2');
  78.    }
  79.    $Output=strrev($Output);
  80.  }
  81.  return(($Output!='')?$Output:'0');
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. // IP original
  97. $ip4 = "192.168.1.35";
  98. $ip6 = "FEBC:A574:382B:23C1:AA49:4592:4EFE:9982";
  99. $ipOriginal = strtolower($ip4);
  100.  
  101.  
  102. if(strpos($ipOriginal, ":") === false){
  103. // IP4
  104.     $ipInt = sprintf("%u", ip2long($ipOriginal));
  105.     // Guardamos pero solo usando la columna primera de IP, la otra se deja vacia (NULL)
  106.     echo $ipInt;
  107. }else{
  108. // IP6
  109.  
  110.     // Convertimos la IP a binario
  111.     $ipBinaria = ip2bin($ipOriginal);
  112.  
  113.     // Dividimos la cadena en dos partes iguales de 64 caracteres
  114.     $parte1 = substr($ipBinaria, 0, 64);
  115.     $parte2 = substr($ipBinaria, 64);
  116.  
  117.     // Pasamos esas partes al numero decimal correspondiente
  118.     $parte1Decimal = BCBin2Dec($parte1);
  119.     $parte2Decimal = BCBin2Dec($parte2);
  120.  
  121.     // Guardamos en la base de datos la parte 1 y la parte 2
  122.     // mysql_query("INSERT INTO accesos....");
  123.     /*
  124.     echo $parte1Decimal.'<br>';
  125.     echo $parte2Decimal;
  126.     */
  127.  
  128.     // Volvemos a crear la IP desde los valores de la base de datos, ejemplo:
  129.     $ipBDparte1 = '18355728099832439745';
  130.     $ipBDparte2 = '12270415154440477058';
  131.     $parte1BinariaRetorno = BCDec2Bin($ipBDparte1);
  132.     $parte2BinariaRetorno = BCDec2Bin($ipBDparte2);
  133.    
  134.     echo $ipOriginal.'<br>';
  135.     echo bin2ip($parte1BinariaRetorno.$parte2BinariaRetorno);
  136.    
  137. }
  138.  
  139.  
  140.  
  141. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement