2016-04-14 1 views
1

J'ai essayé de changer ce code php en mot_de_passe quand l'utilisateur entre son mot de passe s'il veut le changer, mais il ne correspondra jamais parce que le mot pssword stocké dans ma base de données est haché pendant le registre. Il entrée pour correspondre avec le mot de passe haché sur DB qui ne fonctionne pas.Comment changer le mot de passe en utilisant password_hash?

Je sais que je devrais utiliser password_verify mais je ne sais pas quelle partie de ce code dois-je insérer?

<?php 
    include 'session.php'; 
    $db = new mysqli('localhost', 'root', '', 'alumni'); 
    if(isset($_POST['submit'])): 
    extract($_POST); 


    $user_check=$_SESSION['login_user']; 

    $old_pwd=$_POST['old_password']; 
    $pwd=$_POST['password']; 
    $c_pwd=$_POST['confirm_pwd']; 
    if($old_pwd!="" && $pwd!="" && $c_pwd!="") : 


    if($pwd == $c_pwd) : 
    if($pwd!=$old_pwd) : 
    $sql="SELECT * FROM `alumni` WHERE `username`='$user_check' AND `password` ='$old_pwd'"; 
    $db_check=$db->query($sql); 
    $count=mysqli_num_rows($db_check); 
    if($count==1) : 
    $fetch=$db->query("UPDATE `alumni` SET `password` = '$pwd' WHERE `username`='$user_check'"); 
    $old_pwd=''; $pwd =''; $c_pwd = ''; 
    $msg_sucess = "Password successfully updated!"; 
    else: 
    $error = "Old password is incorrect. Please try again."; 
    endif; 
    else : 
    $error = "Old password and new password are the same. Please try again."; 
    endif; 
    else: 
    $error = "New password and confirm password do not match."; 
    endif; 
    else : 
    $error = "Please fill all the fields"; 
    endif; 
    endif; 
?> 
+0

Oubliez de commencer la session en haut !! Vous devez ajouter 'session_start()' en haut de votre page !! – Saty

+1

@Saty vous voulez dire '' session_start() ''. – jmattheis

+0

AHHH: mon erreur @JannisM oui je veux dire que !! merci – Saty

Répondre

0

Essayez cette

<?php 
    include 'session.php'; 
    session_start(); 
    $db = new mysqli('localhost', 'root', '', 'alumni'); 
    if(isset($_POST['submit'])): 
    extract($_POST); 


    $user_check=$_SESSION['login_user']; 

    $old_pwd=$_POST['old_password']; 
    $pwd=$_POST['password']; 
    $c_pwd=$_POST['confirm_pwd']; 
    if($old_pwd!="" && $pwd!="" && $c_pwd!="") : 


    if($pwd == $c_pwd) : 
    if($pwd!=$old_pwd) : 
    $sql="SELECT * FROM `alumni` WHERE `username`='$user_check' AND `password` =PASSWORD($old_pwd)"; 
    $db_check=$db->query($sql); 
    $count=mysqli_num_rows($db_check); 
    if($count==1) : 
    $fetch=$db->query("UPDATE `alumni` SET `password` = PASSWORD($pwd) WHERE `username`='$user_check'"); 
    $old_pwd=''; $pwd =''; $c_pwd = ''; 
    $msg_sucess = "Password successfully updated!"; 
    else: 
    $error = "Old password is incorrect. Please try again."; 
    endif; 
    else : 
    $error = "Old password and new password are the same. Please try again."; 
    endif; 
    else: 
    $error = "New password and confirm password do not match."; 
    endif; 
    else : 
    $error = "Please fill all the fields"; 
    endif; 
    endif; 
?> 
+3

L'OP utilise ['password_hash()'] (http://php.net/manual/fr/function.password-hash.php) et ['password_verify()'] (http://php.net/manual/fr/function.password-verify.php), pas 'md5()' – Sean

+0

essayez maintenant. J'ai édité ma réponse –

+0

['password_hash()'] (http://php.net/manual/fr/function.password-verify.php) est une fonction PHP, ** pas ** une fonction MySQL, donc il ne fonctionne pas dans la requête comme ça. Donc, il doit soit être haché avant la requête, à savoir. '$ old_pwd = password_hash ($ _ POST ['old_password']);', ou vous devez le concaténer, c'est-à-dire. '... SET \' mot de passe \ '=" .password_hash ($ pwd). " ... ' – Sean

1

je l'ai enfin travailler! Merci beaucoup d'avoir aidé, surtout à Sir Sean! : D

Je changé ceci:

$sql="SELECT * FROM `alumni` WHERE `username`='$user_check' AND `password` ='$old_pwd'"; 
$db_check=$db->query($sql); 
$count=mysqli_num_rows($db_check); 

if($count==1) : 
    $fetch=$db->query("UPDATE `alumni` SET `password` = '$pwd' WHERE `username`='$user_check'"); 

à ceci:

$sql=("SELECT * FROM alumni WHERE username='$user_check'"); 
$db_check=$db->query($sql); 
if(password_verify($old_pwd,$db_check->fetch_assoc()['password'])): 
$fetch=$db->query("UPDATE `alumni` SET `password` = '$new_pw' WHERE username`='$user_check'"); 

Et oui, j'ai ajouté une nouvelle variable $ new_pw

$new_pw = password_hash($c_pwd, PASSWORD_DEFAULT); 

Si vous pensez les gars je l'ai fait dans un mauvais sens, s'il vous plaît n'hésitez pas à commenter. Merci :)