Website - User changes password even if there is an error between the new pass and the confirmation of the password

Asked

Viewed 28 times

0

When a user wants to change his password there are 3 Labels, 1 for the old password and the other 2 for the new password and for its confirmation. If I enter all the fields correctly, everything goes as I want, but if the new password and its confirmation are different, changes the password as if there was no error, when there is one.I’ve tried some solutions but nothing seems to work.

Php Code

<?php

session_start();

if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){

}
else{
    header("location: index.php");
}
require_once ('ligaDB.php');


if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['send'])) {

require_once ('ligaDB.php'); // Connect to the db.


$email = $con->real_escape_string(trim($_POST['email']));
$p = $con->real_escape_string(trim($_POST['pass']));

$id = $_SESSION['user_id'];

This is what I want you to do when the passwords don’t match but don’t funicona, it doesn’t seem to go through this if.

if ($_POST['pass1'] != $_POST['pass2']) {
     $frase = "A sua senha não foi atualizada.";
     header("location: mudar_passe.php?erro=$frase");

} else {
    $np = $con->real_escape_string(trim($_POST['pass1']));
}


// verifica se a senha antiga está correta
$sql = "SELECT user_id FROM utilizadores WHERE (user_id='$id' AND user_senha=SHA1('$p') )";
$result = $con->query($sql);

if ($result->num_rows == 1) {  // a senha está correta

    $row = $result->fetch_array();

    // atualiza a senha com o novo valor inserido
    $q = "UPDATE utilizadores SET user_senha=SHA1('$np') WHERE user_id=$row[0]";        
    $r = $con->query($q);

    if ($con->affected_rows == 1) { // Se foi afetada/atualizada uma linha

Comes right here with the error and successfully edits the password.

        $frase = "A sua senha foi atualizada.";
        header("location: login_index.php?erro=$frase");

    } else {

        // Debugging message:
        //echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';

        $frase = "A senha não foi atualizada, ocorreu um erro no sistema.";
        header("location: mudar_passe.php?erro=$frase");

    }

    include ('includes/footer.php'); 
    exit();

} else { // Há problemas com os dados na base de dados, ou os dados não existem ou há email e senha repetidos

    $frase = "A senha está incorreta!!.";
    header("location: mudar_passe.php?erro=$frase");
}

$con->close(); // fecha a ligação à base de dados.

} 
?>

HTML Code

<div class="container-fluid">
        <div class="row">

                <div class="page-header"><br>
                    <center><h2>Alterar Palavra-Passe</h2></center>
                </div>
                <center><form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                    <div class="form-group">
                        <label for="InputPasse">Palavra-Passe Atual</label><br>
                        <input id="InputPasse" name="pass" required="required" type="password" />
                    </div>
                    <div class="form-group">
                        <label for="InputPasse">Nova Palavra-Passe</label><br>
                        <input id="InputPasse" name="pass1" required="required" type="password" />
                    </div>
                    <div class="form-group">
                        <label for="InputPasse">Nova Palavra-Passe</label><br>
                        <input id="InputPasse" name="pass2" required="required" type="password" />
                    </div>
                    <input type="submit" name="send" class="btn btn-primary" value="Alterar">
                        <a href="login_index.php" class="btn btn-default">Cancelar</a>
                    </form><br><br><br>
                    <p>Boas <b><?php echo $_SESSION['username']?></b>, aqui poderás alterar a tua palavra-passe. </p></center>
        </div>        
    </div>

1 answer

0


I made a modification to your code, left the two checks in one IF.

// verifica se a senha antiga está correta
$sql = "SELECT user_id FROM utilizadores WHERE (user_id='$id' AND user_senha=SHA1('$p') )";
$result = $con->query($sql);

if ($result->num_rows == 1 && $_POST['pass1'] == $_POST['pass2']) {
     $np = $con->real_escape_string(trim($_POST['pass1']));

     $row = $result->fetch_array();

    // atualiza a senha com o novo valor inserido
    $q = "UPDATE utilizadores SET user_senha=SHA1('$np') WHERE user_id=$row[0]";        
    $r = $con->query($q);

    if ($con->affected_rows == 1) { // Se foi afetada/atualizada uma linha
        $frase = "A sua senha foi atualizada.";
        header("location: login_index.php?erro=$frase");
    } else {
         $frase = "A senha não foi atualizada, ocorreu um erro no sistema.";
         header("location: mudar_passe.php?erro=$frase");
    }

   include ('includes/footer.php'); 
    exit();
} else {
     $frase = "A sua senha não foi atualizada.";
     header("location: mudar_passe.php?erro=$frase");
}
  • Thank you very much :) It worked. I’ve figured out what was wrong. Again, thank you very much :D

Browser other questions tagged

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