Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Consulta CPF</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body bgcolor='black'>
- <div class="container">
- <form action="confirmacpf.php" method="GET">
- Digite seu CPF:<input type="text" name="cpf"/>
- <input type="submit" value="Send">
- </form>
- </div>
- </body>
- </html>
- <?php
- $cpf = $_GET['cpf'];
- //$cpf = formatCPF($_GET['cpf']);
- //echo "CPF $cpf Formatado";
- verificaCPF($cpf);
- function formatCPF($cpf) {
- $chars = ['.', '-'];
- $newcpf = str_replace($chars, '', $cpf);
- return $newcpf;
- }
- function verificaCPF($cpf) {
- $cpf2 = formatCPF($cpf);
- $cut = substr($cpf2, 0, -2);
- $digito = substr($cpf2, -2);
- $tam = strlen($cut);
- $sumd1 = [];
- $sumd2 = [];
- for ($i = 0; $i < $tam; $i++) {
- $n = $i + 1;
- $chars = substr($cut, $i, 1);
- $sumd1 [$i] = $chars * $n;
- $sumd2 [$i] = $chars * $i;
- }
- $d1 = array_sum($sumd1); // 232
- $d2 = array_sum($sumd2) + ($d1 * 9); //2277
- $rd1 = valorD($d1); //1
- $rd2 = valorD($d2); // 10 -> 0
- if ($rd1 == substr($digito, 0, -1) && ($rd2 == substr($digito, 1))) {
- echo 'CPF Válido';
- } else {
- echo 'CPF Inválido';
- }
- echo " $rd1 - $rd2";
- }
- function valorD($num) {
- $num %= 11;
- if ($num == 10) {
- return $num = 0;
- } else {
- return $num;
- }
- }
- //d1 = Array ( 4 , 2 , 12 , 36, 15 , 48 , 35 , 8 , 72 )
- //d2 = Array ( 0 , 1 , 8 , 27 , 12 , 40 , 30 , 7 , 64 )
- //for($i=1;$i<8;$i++)
- //d1 = (a x 1) + (b x 2) + (c x 3) + (d x 4) + (e x 5) + (f x 6) + (g * 7) + (h * 8) + (i * 9);
- //d2 = (a x 0) + (b x 1) + (c x 2) + (d x 3) + (e x 4) + (f x 5) + (g x 6) + (h x 7) + (i x 8) + (d1 x 9)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement