Password Reset

0

I am making an app but I am having difficulty implementing the password reset option, I use a web server, I would like to know the best password reset option:

1) Secret question chosen by the user at time of registration;

2) Reset porting message sending to user email.

Note: I do not know how to implement the second option. How can I make a php code to send an email automatically to the user

    
asked by anonymous 01.08.2017 / 18:31

1 answer

0

My friend, the second option is certainly the safest and most recommended. Sending an email from the server via PhP is relatively simple ...

 <?php
  include('../conexao/conexao.php');
  date_default_timezone_set('America/Sao_Paulo');
  ini_set('smtp_port', '587');
  if(isset($_POST['acao']) && $_POST['acao'] == 'recuperar'){
    //filtra caracteres especiais
    $email = strip_tags(filter_input(INPUT_POST, 'emailRecupera', FILTER_SANITIZE_STRING));
    $sql = "SELECT * FROM usuario WHERE email = '$email'";
    $verificar = mysqli_query($conexao,$sql);
    if(mysqli_num_rows($verificar) == 1){
      $codigo = base64_encode($email);
      $data_expirar = date('Y-m-d H:i:s', strtotime('+1 day'));
      $mensagem ="<html>
                    <head></head>
                      <body>
                        <h1>Reservas de Salas - ENE</h1><br>
                        <h2>Recebemos uma tentativa de recuperação de senha para este e-mail.</h2>
                        <p>Caso não tenha solicitado, por favor, desconsidere este e-mail. Caso contrário clique no link abaixo para alterar a senha.<br><br>
                        <a href= http://homol.redes.unb.br/ptr012017-B-grupoA/recuperar/recuperar.php?codigo=".$codigo.">Recuperar Senha</a></p><br><br>
                        <p>Departamento de Engenharia Elétrica</p>
                      </body>
                  </html>";
      $email_remetente = '[email protected]';
      $assunto = 'Recuperação de senha';
      $headers = "Content-type: text/html; charset=utf-8\r\n";
      $inserir =  mysqli_query($conexao,"INSERT INTO codigos SET codigo = '$codigo', data = '$data_expirar'");
      if($inserir) {
        if(mail("$email","$assunto","$mensagem", $headers, "-f$email_remetente")){
          header('Location: ../index.html?SUCESS=2'); //Verifique seu e-mail para obter nova senha!
        }
      }
    } else {
      header('Location: ../index.html?ERROR=6'); //  E-mail digitado não está cadastrado.
    }
  }
?>

Remembering that your PhP has been configured to send email. It's worth remembering that the link you send to the user has to be a new session for security reasons.

    
01.08.2017 / 18:57