Advertisement
Guest User

confirmacpf

a guest
Mar 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.91 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4.     <head>
  5.         <title>Consulta CPF</title>
  6.         <meta charset="UTF-8">
  7.         <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8.     </head>
  9.     <body bgcolor='black'>
  10.         <div class="container">
  11.             <form action="confirmacpf.php" method="GET">
  12.                 Digite seu CPF:<input type="text" name="cpf"/>
  13.                 <input type="submit" value="Send">
  14.             </form>
  15.         </div>
  16.     </body>
  17. </html>
  18. <?php
  19. $cpf = $_GET['cpf'];
  20.  
  21.  
  22. //$cpf = formatCPF($_GET['cpf']);
  23. //echo "CPF $cpf Formatado";
  24.  
  25. verificaCPF($cpf);
  26.  
  27. function formatCPF($cpf) {
  28.     $chars = ['.', '-'];
  29.     $newcpf = str_replace($chars, '', $cpf);
  30.     return $newcpf;
  31. }
  32.  
  33. function verificaCPF($cpf) {
  34.     $cpf2 = formatCPF($cpf);
  35.     $cut = substr($cpf2, 0, -2);
  36.     $digito = substr($cpf2, -2);
  37.     $tam = strlen($cut);
  38.  
  39.     $sumd1 = [];
  40.     $sumd2 = [];
  41.     for ($i = 0; $i < $tam; $i++) {
  42.         $n = $i + 1;
  43.         $chars = substr($cut, $i, 1);
  44.         $sumd1 [$i] = $chars * $n;
  45.         $sumd2 [$i] = $chars * $i;
  46.     }
  47.  
  48.     $d1 = array_sum($sumd1); // 232
  49.     $d2 = array_sum($sumd2) + ($d1 * 9); //2277
  50.  
  51.     $rd1 = valorD($d1); //1
  52.     $rd2 = valorD($d2); // 10 -> 0
  53.  
  54.     if ($rd1 == substr($digito, 0, -1) && ($rd2 == substr($digito, 1))) {
  55.         echo 'CPF Válido';
  56.     } else {
  57.         echo 'CPF Inválido';
  58.     }
  59.     echo " $rd1 - $rd2";
  60. }
  61.  
  62. function valorD($num) {
  63.     $num %= 11;
  64.     if ($num == 10) {
  65.         return $num = 0;
  66.     } else {
  67.         return $num;
  68.     }
  69. }
  70.  
  71. //d1 = Array (  4 , 2 , 12 , 36, 15 , 48 , 35 , 8 , 72 )
  72. //d2 = Array (  0 , 1 , 8 , 27 , 12 , 40 , 30 , 7 , 64 )
  73.  
  74.  
  75.  
  76. //for($i=1;$i<8;$i++)
  77.  
  78. //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);
  79. //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