How to encrypt the password in the registration of a new user and how to recognize it in the login of this user?

Asked

Viewed 552 times

0

this is the code that processes the register

    <?php
    include_once("conexao.php");

    $nome = $_POST['nome'];
    $celular = $_POST['celular'];
    $email = $_POST['email'];
    $senha = $_POST['senha'];

    $sql = "INSERT INTO usuarios (nome,celular,email,senha) VALUES ('$nome','$celular','$email','$senha')";

    $salvar = mysqli_query($conexao,$sql);

    $linhas = mysqli_affected_rows($conexao);

    mysqli_close($conexao); ?>

<!DOCTYPE HTML>

<html lang="pt-br">

    <head>
        <title>Loja - Cadastro</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0" />
        <meta name="theme-color" content="#FF0000">
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="Cache-Control" content="no-store" />
        <link rel="icon" href="../imagens/favicon.png" type="image/png" />
        <link href="../css/style.css" type="text/css" rel="stylesheet" media="screen,projection" />
        <link href="../css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection" />
        <link href="../css/animate.min.css" type="text/css" rel="stylesheet" media="all">
    </head>
<body>
    <?php
        include("navbarforms.php");
    ?>

    <div class="container-fluid">

    <?php
        if($linhas == 1){
        echo "<h4>Cadastro efetuado com sucesso!</h4>";
        header("Location: ../entrar.php");
        } else {
        echo "<h5 class='red-text'>Usuário <strong>$email</strong> já existente no sistema</h5><br><p class='text-blue mdfont'>Redirecionando para a pagina de cadastro...</p>";
        header("refresh: 5; url=../cadastrar.php");
        }
    ?>

    </div>
</body>

</html>

and this is the code that processes the login

    <?php
session_start();
include("conexao.php");

if (empty($_POST['email']) || empty($_POST['senha'])) {
    header("Location: ../entrar.php;");
    exit();
}

$email = mysqli_real_escape_string($conexao, $_POST['email']);
$senha = mysqli_real_escape_string($conexao, $_POST['senha']);

$query = "SELECT email, senha FROM usuarios WHERE email = '$email' AND senha = '$senha'";

$result = mysqli_query($conexao, $query);

$row = mysqli_num_rows($result);

if ($row == 1) {
    $_SESSION['email'] = $email;
    header("location: ../index.php");
    exit();
} else {
    $_SESSION['nao_autenticado'] = true;
    header("location: ../entrar.php");
    exit();
}

?>

I would like to know how to send the encrypted password and then I can log in with the password entered in the registration.

1 answer

-1

Take the field and use the encryption you used to send it to SELECT ex in the register you used md5 in $_POST['password']; thus md5($_POST['password']); at the time of logging in you do the same thing, so when you check the encrypted password, you will be sending the same password as the database if the user is typed correctly

Browser other questions tagged

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