Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- BASE DE DATOS
- 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
- CREATE TABLE IF NOT EXISTS `accesos` (
- `ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
- `CONEXION` int(11) unsigned NOT NULL,
- `ULTIMA_ACTUALIZACION` int(11) unsigned NOT NULL,
- `ID_USUARIO` int(10) unsigned NOT NULL,
- `IP1` bigint(20) unsigned NOT NULL,
- `IP2` bigint(20) unsigned DEFAULT NULL,
- PRIMARY KEY (`ID`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
- */
- // Convierte una ip4 o ip6 a binario
- function ip2bin($ip){
- if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false)
- return base_convert(ip2long($ip),10,2);
- if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false)
- return false;
- if(($ip_n = inet_pton($ip)) === false) return false;
- $bits = 15; // 16 x 8 bit = 128bit (ipv6)
- while ($bits >= 0){
- $bin = sprintf("%08b",(ord($ip_n[$bits])));
- $ipbin = $bin.$ipbin;
- $bits--;
- }
- return $ipbin;
- }
- // Convierte un binario de ip4 o ip6 a su presentacion estandar
- function bin2ip($bin){
- if(strlen($bin) <= 32) // 32bits (ipv4)
- return long2ip(base_convert($bin,2,10));
- if(strlen($bin) != 128)
- return false;
- $pad = 128 - strlen($bin);
- for ($i = 1; $i <= $pad; $i++)
- {
- $bin = "0".$bin;
- }
- $bits = 0;
- while ($bits <= 7)
- {
- $bin_part = substr($bin,($bits*16),16);
- $ipv6 .= dechex(bindec($bin_part)).":";
- $bits++;
- }
- return inet_ntop(inet_pton(substr($ipv6,0,-1)));
- }
- // Convierte de binario a decimal sin el E+
- function BCBin2Dec($Input='') {
- $Output='0';
- if(preg_match("/^[01]+$/",$Input)) {
- for($i=0;$i<strlen($Input);$i++)
- $Output=BCAdd(BCMul($Output,'2'),$Input{$i});
- }
- return($Output);
- }
- // Convertimos de nuevo a binario desde el decimal dado con la funcion de arriba
- function BCDec2Bin($Input='') {
- $Output='';
- if(preg_match("/^\d+$/",$Input)) {
- while($Input!='0') {
- $Output.=chr(48+($Input{strlen($Input)-1}%2));
- $Input=BCDiv($Input,'2');
- }
- $Output=strrev($Output);
- }
- return(($Output!='')?$Output:'0');
- }
- // IP original
- $ip4 = "192.168.1.35";
- $ip6 = "FEBC:A574:382B:23C1:AA49:4592:4EFE:9982";
- $ipOriginal = strtolower($ip4);
- if(strpos($ipOriginal, ":") === false){
- // IP4
- $ipInt = sprintf("%u", ip2long($ipOriginal));
- // Guardamos pero solo usando la columna primera de IP, la otra se deja vacia (NULL)
- echo $ipInt;
- }else{
- // IP6
- // Convertimos la IP a binario
- $ipBinaria = ip2bin($ipOriginal);
- // Dividimos la cadena en dos partes iguales de 64 caracteres
- $parte1 = substr($ipBinaria, 0, 64);
- $parte2 = substr($ipBinaria, 64);
- // Pasamos esas partes al numero decimal correspondiente
- $parte1Decimal = BCBin2Dec($parte1);
- $parte2Decimal = BCBin2Dec($parte2);
- // Guardamos en la base de datos la parte 1 y la parte 2
- // mysql_query("INSERT INTO accesos....");
- /*
- echo $parte1Decimal.'<br>';
- echo $parte2Decimal;
- */
- // Volvemos a crear la IP desde los valores de la base de datos, ejemplo:
- $ipBDparte1 = '18355728099832439745';
- $ipBDparte2 = '12270415154440477058';
- $parte1BinariaRetorno = BCDec2Bin($ipBDparte1);
- $parte2BinariaRetorno = BCDec2Bin($ipBDparte2);
- echo $ipOriginal.'<br>';
- echo bin2ip($parte1BinariaRetorno.$parte2BinariaRetorno);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement