PHP CPF validator

Asked

Viewed 1,405 times

2

Good afternoon, everyone,

I’d like to know what’s wrong with my Cpf validator, made in PHP. What happens, it validates my number, my mother’s, brother’s, girlfriend’s and grandma’s. But, when I test some CPF I took from a Cpf generator online, it valid some and others not.

Consider receiving the form CPF value via POST from a number field (No scores)

<?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 stylish. But please I want to know the error of this code, and don’t get one of the internet ready.

1 answer

3


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

Just correct the value of $i in both ties for of the code, changing $i < 0 and $ii < 0 for $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>

  • Wow, it was so long ago that I only used foreach that I didn’t even realize that the mistake was in mine is misspelled. Thank you very much ! It really was that. D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.