Jump to content
Fórum Script Brasil
  • 0

Cálculando a rede e convertendo decimal p/Binário


Anderson Guimarães

Question

Calcula rede

<?php
//$ip_inf   = $_POST["ip_inf"];//variavel recebe o IP de outra pagina...
//$mask_inf = $_POST["mask_inf"];//variavel recebe a máscara de outra pagina...

$ip_inf      = "192.168.1.33";
$mask_inf = "255.255.255.128";
$rede_inf = "0.0.0.0";

$IP_D   = explode(".", $ip_inf); //Matriz de 4 que contem os valores decimais dos octetos...
$MASC_D = explode(".", $mask_inf);
$REDE_D = explode(".", $rede_inf);
$contMASK_decimal = 0;
for($i=0; $i<4; $i++) //for 1
  {
   if ($i != 0)
     {
      $IPconvertido = $IPconvertido.".";
      $maskconvertido = $maskconvertido.".";
     }
   $contMASK[$i] = 0;
   $Aux_IP_D   = $IP_D[$i];
   $Aux_MASC_D = $MASC_D[$i];
       for ($l=7; $l>-1; $l--)  //for 2
       {
        $oct_ip_BIN[$l] = $Aux_IP_D%2;
        $Aux_IP_D       = floor($Aux_IP_D/2);
        $oct_ma_BIN[$l] = $Aux_MASC_D%2;
        
           if ($oct_ma_BIN[$l] == "1")
             {
               $contMASK_decimal = $contMASK_decimal + 1;
               $contMASK[$i]     = $contMASK[$i] + 1;
             }
        
        $Aux_MASC_D     = floor($Aux_MASC_D/2);
        $oct_re_BIN[$l] = ($oct_ip_BIN[$l]*$oct_ma_BIN[$l]);
         switch ($l)
           {
             case 0:
                 $REDE_D[$i] = (($REDE_D[$i])+($oct_re_BIN[$l]*128));
                 break;
             case 1:
                 $REDE_D[$i] = (($REDE_D[$i])+($oct_re_BIN[$l]*64));
                 break;
             case 2:
                 $REDE_D[$i] = (($REDE_D[$i])+($oct_re_BIN[$l]*32));
                 break;
             case 3:
                 $REDE_D[$i] = (($REDE_D[$i])+($oct_re_BIN[$l]*16));
                 break;
             case 4:
                 $REDE_D[$i] = (($REDE_D[$i])+($oct_re_BIN[$l]*8));
                 break;
             case 5:
                 $REDE_D[$i] = (($REDE_D[$i])+($oct_re_BIN[$l]*4));
                 break;
             case 6:
                 $REDE_D[$i] = (($REDE_D[$i])+($oct_re_BIN[$l]*2));
                 break;
             case 7:
                 $REDE_D[$i] = (($REDE_D[$i])+($oct_re_BIN[$l]*1));
                 break;
           }//fim do switch case 1
       }//fim do for 2
       //FOR para imprimir os octeto convertido
        for ($x=0; $x<8; $x++)  //for 2
         {
           $IPconvertido   = $IPconvertido.$oct_ip_BIN[$x];
           $maskconvertido = $maskconvertido.$oct_ma_BIN[$x];
         }    
       
       
  }//fim do for 1
echo "<HTML><HEAD><TITLE>Calculo de rede</TITLE></HEAD>";
echo "<br><h2><p align='center'>:: Resultado do Cálculo de Rede ::</p></h2><br>";

$hosts       = pow(2, (32-$contMASK_decimal));
$hostsv      = $hosts - 2;

if ($contMASK_decimal > 23)
  {
    $broadcast = $REDE_D[3] + $hosts - 1;
  }

if ($contMASK_decimal > 23)
  {
    $broadcast = $REDE_D[3] + $hosts - 1;
    $broadcast = $REDE_D[0].".".$REDE_D[1].".".$REDE_D[2].".".$broadcast;
    $ipfinal   = $REDE_D[3] + $hosts - 2;
    $ipfinal   = $REDE_D[0].".".$REDE_D[1].".".$REDE_D[2].".".$ipfinal;
  }

if ($contMASK_decimal > 15 and $contMASK_decimal < 24)
  {
    $tempaux   = 24 - $contMASK_decimal;
    $tempaux   = ((($REDE_D[2]) + (pow(2, $tempaux))) - 1);
    $broadcast = $REDE_D[0].".".$REDE_D[1].".".$tempaux.".255";
    $ipfinal   = $REDE_D[0].".".$REDE_D[1].".".$tempaux.".254";
  }

if ($contMASK_decimal > 7 and $contMASK_decimal < 16)
  {
    $tempaux   = 16 - $contMASK_decimal;
    $tempaux   = ((($REDE_D[1]) + (pow(2, $tempaux))) - 1);
    $broadcast = $REDE_D[0].".".$tempaux.".255.255";
    $ipfinal   = $REDE_D[0].".".$tempaux.".255.254";
  }

if ($contMASK_decimal > 1 and $contMASK_decimal < 8)
  {
    $tempaux   = 8 - $contMASK_decimal;
    $tempaux   = ((($REDE_D[0]) + (pow(2, $tempaux))) - 1);
    $broadcast = $tempaux.".255.255.255";
    $ipfinal   = $tempaux.".255.255.254";
  }

$ipinicial = $REDE_D[3] + 1;
$ipinicial = $REDE_D[0].".".$REDE_D[1].".".$REDE_D[2].".".$ipinicial;
echo "<table border='0' width='50%'>";
echo "<tr>";
echo "<td><b>IP Informado:</b></td>";
echo "<td>$ip_inf</td>";
echo "</tr>";
echo "<tr>";
echo "<td><b>Máscara Informada:</b></td>";
echo "<td>$mask_inf   =   / $contMASK_decimal</td>";
echo "</tr>";
echo "<tr>";
echo "<td><b>IP Binário:</b></td>";
echo "<td>$IPconvertido</td>";
echo "</tr>";
echo "<tr>";
echo "<td><b>Máscara Binária:</b></td>";
echo "<td>$maskconvertido</td>";
echo "</tr>";
echo "<tr>";
echo "<td><b>Rede:</b></td>";
echo "<td>$REDE_D[0].$REDE_D[1].$REDE_D[2].$REDE_D[3]</td>";
echo "</tr>";
echo "<tr>";
echo "<td><b>N. de Hosts:</b></td>";
echo "<td>$hosts</td>";
echo "</tr>";
echo "<tr>";
echo "<td><b>Hosts Válidos:</b></td>";
echo "<td>$hostsv</td>";
echo "</tr>";
echo "<tr>";
echo "<td><b>IP inicial:</b></td>";
echo "<td>$ipinicial</td>";
echo "</tr>";
echo "<tr>";
echo "<td><b>IP Final:</b></td>";
echo "<td>$ipfinal</td>";
echo "</tr>";
echo "<tr>";
echo "<td><b>Broadcast:</b></td>";
echo "<td>$broadcast</td>";
echo "</tr>";
echo "</table>";
?>

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      651.8k
×
×
  • Create New...