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.
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
– André Molinari