CPF Validator in PHP

1

Good afternoon guys,

I'd like to know what's wrong with my cpf validator, made in PHP language. What happens, he validates my CPF, from my mother, brother, girlfriend and grandmother. But as I test some CPFs I got from an online cpf generator, it does valid some and not others.

Consider getting the form CPF value via POST from a number field

<?php 
require_once ("cabecalho.php");
$cpf = $_POST['cpf'];

if (strlen($cpf) != 11) {
    $_SESSION["danger"] = "O CPF deve conter 11 digítos. Preencha novamente.";
    header("Location: cpf.php");
    die();
}

$verificaJ = $cpf[9];
$verificaK = $cpf[10];

$J_letra = 10;
$J_array = array();
for ($i < 0; $i <= 8; $i++){
    $J_array[] = $cpf[$i] * $J_letra;
    $J_letra--;
    $J_soma = $J_soma + $J_array[$i];
}

$J_resto = $J_soma % 11;
$J_subtracao = 11 - $J_resto;

if ($J_subtracao > 9) {
    $J = 0;
    echo "J:" . $J . "<br> ";
} else {
    $J = $J_subtracao;
    echo "J else:" . $J . "<br>";
}

//Conseguindo K
$K_letra = 11;
$K_array = array();
for ($ii < 0; $ii <= 9; $ii++){
    $K_array[] = $cpf[$ii] * $K_letra;
    $K_letra--;
    $K_soma = $K_soma + $K_array[$ii];
}

$K_resto = $K_soma % 11;
$K_subtracao = 11 - $K_resto;
if ($K_subtracao > 9) {
    $K = 0;
    echo "K:" . $K . "<br> ";

} else {
    $K = $K_subtracao;
    echo "K:" . $K . "<br> ";

}

if ($verificaJ == $J && $verificaK == $K){
   echo "CPF VÁLIDO";
} else {
  echo "CPF INVÁLIDO";
}

Ps: I know the code is not very elegant. But please, I want to know the error of this code, and do not get one from the internet ready.

    
asked by anonymous 23.09.2018 / 23:26

1 answer

1

The error is in the value of $i in the for loop. The assigned value should be $i = 0 and not $i < 0 . With this, the $i only becomes value, and of 1 , after the first loop loop, ignoring the index 0 of the array, resulting in the wrong sum of the calculations.

Just correct the value of $i in the two for loops of the code, changing $i < 0 and $ii < 0 by $i = 0 and $ii = 0 respectively.

See the same code running in JavaScript (valid CPFs were generated on a CPF generator site):

function validaCPF($cpf){

   $verificaJ = $cpf[9];
   $verificaK = $cpf[10];
   
   $J_letra = 10;
   $J_array = [];
   $J_soma = 0;
   for ($i = 0; $i <= 8; $i++){
       $J_array.push($cpf[$i] * $J_letra);
       $J_letra--;
       $J_soma = $J_soma + $J_array[$i];
   }
   
   $J_resto = $J_soma % 11;
   $J_subtracao = 11 - $J_resto;
   
   if ($J_subtracao > 9) {
       $J = 0;
   } else {
       $J = $J_subtracao;
   }
   
   //Conseguindo K
   $K_letra = 11;
   $K_array = [];
   $K_soma = 0;
   for ($ii = 0; $ii <= 9; $ii++){
       $K_array.push($cpf[$ii] * $K_letra);
       $K_letra--;
       $K_soma = $K_soma + $K_array[$ii];
   }
   
   $K_resto = $K_soma % 11;
   $K_subtracao = 11 - $K_resto;
   if ($K_subtracao > 9) {
       $K = 0;
   
   } else {
       $K = $K_subtracao;
   
   }
   
   if ($verificaJ == $J && $verificaK == $K){
      return $cpf+": CPF VÁLIDO";
   } else {
     return $cpf+": CPF INVÁLIDO";
   }
}

console.log(validaCPF('59848265007')); // válido
console.log(validaCPF('59848265008')); // inválido
console.log(validaCPF('55688772034')); // válido
console.log(validaCPF('55688772033')); // inválido
<p>Digite um CPF</p>
<input type="text" id="cpf" style="width: 200px; font-size: 30px;">
<button onclick="console.clear(); console.log(validaCPF(document.getElementById('cpf').value))">OK</button>
    
24.09.2018 / 01:24