Foros del Web » Programando para Internet » PHP »

Validar IP/Red 0-255.X.X.1-254/2-32

Estas en el tema de Validar IP/Red 0-255.X.X.1-254/2-32 en el foro de PHP en Foros del Web. Hola Gente que tal !! Tratando de validar el ingreso en un text una IP definiendo que : si serian validas "0.0.0.1" o "255.255.255.254" o ...
  #1 (permalink)  
Antiguo 18/12/2007, 15:16
Avatar de yetrus  
Fecha de Ingreso: marzo-2006
Mensajes: 252
Antigüedad: 18 años, 1 mes
Puntos: 0
Validar IP/Red 0-255.X.X.1-254/2-32

Hola Gente que tal !!

Tratando de validar el ingreso en un text una IP definiendo que :

si serian validas "0.0.0.1" o "255.255.255.254" o "0~255.0~255.0~255.1~254 / 2~32"

y no lo serian

"0.0.0.0" o "255.255.255.255" o 10.10.10.2 /33

Creo que el codigo esta correcto, esta en una funcion que retorna true al ser una IP Valida, pero se parece mucho a mi suegra (reclama por todo) (ordene un poco el codigo)

Alguna idea?
sl2

Código PHP:
function ValidarIP($IP)                   ////////////////////////// validador de IP    #################
{
    
$IPValida false;
    
$IP str_replace(" ","",$IP);
    if (
substr_count($IP,".") == 3)
    {
    if (
substr_count($IP,'/') == 1)  //con red
        
{
            {
            list(
$IP,$Red) = split('[/]',$IP); 
            if
            (
            (
$Red == 32) || 
            (
$Red == 16) || 
            (
$Red ==  8) || 
            (
$Red ==  4) || 
            (
$Red ==  2)
            )    
                {
                list(
$oct1,$oct2,$oct3,$oct4) = split ('[.]',$IP);
                if 
                (
                (
is_numeric($oct1) == true) && 
                (
is_numeric($oct2) == true) && 
                (
is_numeric($oct3) == true) && 
                (
is_numeric($oct4) == true)
                )
                    {
                    if
                    (
                    ((
$oct1 >= 0)&& ($oct1 <= 255)) && 
                    ((
$oct2 >= 0)&& ($oct2 <= 255)) && 
                    ((
$oct3 >= 0)&& ($oct3 <= 255)) && 
                    ((
$otc4 255)&& ($otc4 ))
                    )
                        {
                            
$IPValida true;                        
                        }
                    }
                }    
            }
        }
    if(
substr_count($IP,'/') == )  //sin  Red
            
{
                {
                list(
$oct1,$oct2,$oct3,$oct4) = split ('[.]',$IP);
                    if 
                    (
                    (
is_numeric($oct1) == true) && 
                    (
is_numeric($oct2) == true) && 
                    (
is_numeric($oct3) == true) && 
                    (
is_numeric($oct4) == true )
                    ) 
                    {
                        if 
                        (
                        ((
$oct1 >= 0)&& ($oct1 <= 255)) && 
                        ((
$oct2 >= 0)&& ($oct2 <= 255)) && 
                        ((
$oct3 >= 0)&& ($oct3 <= 255)) && 
                        ((
$otc4 >  0)&& ($otc4 <  255))
                        )
                        {
                            
$IPValida true;                        
                        }
                    }
                }
            }
        }
    
return 
$IPValida;


Última edición por yetrus; 22/12/2007 a las 08:00 Razón: Ordene un poco el codigo
  #2 (permalink)  
Antiguo 23/12/2007, 21:46
Avatar de yetrus  
Fecha de Ingreso: marzo-2006
Mensajes: 252
Antigüedad: 18 años, 1 mes
Puntos: 0
Re: Validar IP/Red 0-255.X.X.1-254/2-32

Bueno al final lo hize yo,
Esta medio mounstruoso y es optimizable, pero funciona, retorna False si no es valida la IP




Código PHP:
function ValidarIP($IP)                   ////////////////////////// validador de IP    #################
{
    
$IPValida false;
    
$IP str_replace(" ","",$IP);    
    
/*
    $largo = strlen($IP);
    $A = array ($IP);
    while($A[i] <= $largo )
    {
    i++;
    
    }
    */
//    if(str_count($IP)=)
    
$IP str_replace("/",".",$IP);


    list(
$oct1,$oct2,$oct3,$oct4,$Red) = split('[.]',$IP); 
        
$oct1=trim($oct1);
        
$oct2=trim($oct2);
        
$oct3=trim($oct3);
        
$oct4=trim($oct4);
        
$Red=trim($Red);

    if (
substr_count($IP,".") == 4)  //con red
        
{
            {
            
            if
            (
            (
$Red == 32) || 
            (
$Red == 16) || 
            (
$Red ==  8) || 
            (
$Red ==  4) || 
            (
$Red ==  2)
            )    
            {
                if (
$oct1 >= )
                    {
                        if(
$oct1 256)
                        {
                            if (
$oct2 >= )
                            {
                                if(
$oct2 256)
                                {
                                    if (
$oct3 >= )
                                    {
                                        if(
$oct3 256)
                                        {
                                            if (
$oct4 )
                                            {
                                                if(
$oct4 255)
                                                {
                                                    
$IPValida true;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
            
    else
    {
        if (
substr_count($IP,".") == 3)    //sin Red 
        
{
                    if 
                    (
                    (
is_numeric($oct1) == true) && 
                    (
is_numeric($oct2) == true) && 
                    (
is_numeric($oct3) == true) && 
                    (
is_numeric($oct4) == true )
                    ) 
                    {
                        if (
$oct1 >= )
                        {
                            if(
$oct1 256)
                            {
                                if (
$oct2 >= )
                                {
                                    if(
$oct2 256)
                                    {
                                        if (
$oct3 >= )
                                        {
                                            if(
$oct3 256)
                                            {
                                                if (
$oct4 )
                                                {
                                                    if(
$oct4 255)
                                                    {
                                                        
$IPValida true;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
            }
        
    }    
return 
$IPValida;


Última edición por yetrus; 26/12/2007 a las 12:31
  #3 (permalink)  
Antiguo 24/12/2007, 08:07
 
Fecha de Ingreso: marzo-2004
Ubicación: España
Mensajes: 185
Antigüedad: 20 años, 1 mes
Puntos: 19
Re: Validar IP/Red 0-255.X.X.1-254/2-32

cuando haces el if ( if ( if ( if ( podrias haber hecho solo un if con varios &&
__________________
Escape Room -

Ios Games
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:47.