Problems with captcha and variable email

Asked

Viewed 125 times

1

<?php     
  if(isset($_SERVER['REQUEST_METHOD']) AND $_SERVER['REQUEST_METHOD'] == 'POST'){

  $nome = $_POST['nome'];
  $sobrenome = $_POST['sobrenome'];
  $email = $_POST['email'];
  $senha = $_POST['senha'];
  $sexo = $_POST['sexo'];

  if($nome == ''){
  echo 'Qual é o seu nome?';
  }elseif(strlen($nome)<3){
   echo'Insira um nome existente';
  }elseif($sobrenome==''){
 echo 'Qual é o seu sobrenome';
  }elseif(strlen($sobrenome)<4){
    echo'Insira um sobrenome existente';
  }elseif($email==''){
  echo 'Insira seu email';
  }elseif(!preg_match("/^[az0-9_\.\-]+@[az0-9_\.\-}*{a-z0-9_\-]+\.[a-z]{2,4}$/i",$email)){   
   echo 'E-mail invalido tente outro';
  }elseif($senha==''){
  echo 'Você precisa ter uma senha';

    }else{

        include('../../sllapsocial/classes/DB.class.php');

        $verificar = DB::getConn()->prepare("SELECT `id` FROM `usuarios` WHERE `email`=?");
        if($verificar->execute(array($email))){
            if($verificar->rowCount()>=0){
         echo 'Este e-mail ja existe';

     }elseif($senha=='' OR strlen($senha)<4){
        echo'Senha fraca Insira mais caracteres';
     }elseif(strtolower($captcha) <> strtolower($_SESSION["captchaCadastro"])){
         echo 'Codigo errado';
     }else{
         $Senha = sha1($senha);
         $nascimento = "$ano-$mes-$dia";
         $Inserir = DB::getConn()->prepare("INSERT INTO `usuarios` SET `email`=?, `senha`=?, `nome`=?, `sobrenome`=?, `sexo`=?, `nascimento`=?, `cadastro`=NOW()");

         if($Inserir->execute(array($email,$senha,$nome,$sobrenome,$sexo,$nascimento))){
        header('Location: ./');  
           }

        }

       }

    }

    }


  ?>
  • Guys I know you are tired of seeing this my form more I never found the problem It is the following the normal registered name and surname the field email appears the following message 'Invalid email try another' then I try another and it occurs progressively ,and the captcha never works

1 answer

6


I refactored your code, making a more linear logic to at least see if you can better visualize where you are having problems.

Anyway, you don’t pay attention to all the recommendations and keep mixing uppercase and lowercase, forgetting ; at the end of the lines, and a number of other problems. Already improved with the $_POST, but it’s best to pack everything you can all at once before posting another question, otherwise we’ll have thousands of them and you’ll hardly advance.

<?php
   require_once( '../../sllapsocial/classes/DB.class.php' );

   if( @$_SERVER['REQUEST_METHOD'] == 'POST' ) {
      $nome =      $_POST['nome'];
      $sobrenome = $_POST['sobrenome'];
      $email =     $_POST['email'];
      $senha =     $_POST['senha'];
      $sexo =      $_POST['sexo'];
      $ano =       $_POST['ano'];
      $mes =       $_POST['mes'];
      $dia =       $_POST['dia'];
      $captcha =   $_POST['captcha'];

      $erro = '';

      if( $nome == '' ) {
         $erro .= 'Qual é o seu nome?<br>';
      } elseif ( strlen( $nome ) < 2 ) {
         $erro .= 'Insira um nome existente<br>';
      }

      if( $sobrenome == '' ) {
         $erro .= 'Qual é o seu sobrenome<br>';
      } elseif( strlen( $sobrenome ) < 2 ) {
         $erro .='Insira um sobrenome existente<br>';
      }

      if( $email == '' ) {
         $erro .= 'Insira seu email';
      } elseif( !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
         $erro .= 'E-mail invalido tente outro<br>';
      }

      if( $senha == ''  OR strlen( $senha ) < 4 ) {
         $erro .= 'Você precisa ter uma senha<br>';
      }

      $verificar = DB::getConn()->prepare( 'SELECT `id` FROM `usuarios` WHERE `email`=?' );
      if( $verificar->execute( array( $email ) ) ) {
         if( $verificar->rowCount() > 0 ){
            $erro .= 'Este e-mail ja existe<br>';
         // } else {
            // Se quiser, tire os comments deste código para testar se chegou aqui
            // $erro .= 'Email livre. Pode remover esse else do código<br>';
         }
      } else {
         $erro .= 'Erro interno ao verificar o e-mail<br>';
      }

      if( strtolower( $captcha ) <> strtolower( $_SESSION["captchaCadastro"] ) ) {
         $erro .= 'Codigo errado<br>';
      }

      if( $erro === '' ) {
         $senha = sha1($senha);
         $nascimento = "$ano-$mes-$dia";
         $inserir = DB::getConn()->prepare( 'INSERT INTO `usuarios` SET `email`=?, `senha`=?, `nome`=?, `sobrenome`=?, `sexo`=?, `nascimento`=?, `cadastro`=NOW()' );
         if( $inserir->execute( array( $email, $senha, $nome, $sobrenome, $sexo, $nascimento ) ) ) {
            {
               header('Location: /');
            }
         }
      }
      die( $erro );
   }
?>

The code continues with other problems, such as vulnerability to SQL Injection, but is more linear. It could improve a lot, but in the current situation it is good that it is easier to understand than optimized.

I changed the way your application error messages appear, they accumulate in the variable $erro and are shown at the end, so you first solve the syntax problems, and then the logic problems.

Another thing, the way you restrict the size of the name and the last name, there’s gonna be a lot of angry Chinese who won’t be able to use your system. And the email validation I replaced with a filter_var, but the ideal is to check only if you have an arroba and a point on the right side of it, not to delete valid emails.

  • 4

    I think Chinese will hardly use this system :D

  • @Thank you very much I was sleeping badly because of this form '-'

  • 1

    @Lamborghiniaventador gave an edited to solve the issue you posted recently, that "I" was to be a debug, and in the end forgot to put an explanation in the code. >= was wrong, and I copied it from the original without realizing it.

  • Very good @Bacco all this ok , finally the form shows no error :)

Browser other questions tagged

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