Ver Mensaje Individual
  #2 (permalink)  
Antiguo 23/12/2007, 21:46
Avatar de yetrus
yetrus
 
Fecha de Ingreso: marzo-2006
Mensajes: 252
Antigüedad: 19 años
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