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.