【发布时间】:2016-04-14 05:22:33
【问题描述】:
我尝试在用户输入密码时将这个 php 代码更改为 password_hash,如果他想更改它,但它永远不会匹配,因为存储在我的数据库中的 pssword 在注册期间被散列,所以我需要旧的 pw他输入的内容与 db 上不起作用的散列密码相匹配。
我知道我应该使用 password_verify 但我不知道我应该插入这段代码的哪一部分?
<?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;
?>
【问题讨论】:
-
忘记在顶部开始会话!您需要在页面顶部添加
session_start()!! -
@Saty 你的意思是
session_start()。 -
AHHH:我的错误@JannisM 是的,我的意思是!谢谢
-
我只是忘记复制了,抱歉。我已经编辑过了,但我的问题还是一样@Saty
-
好吧,你可以选择
password,而不是SELECT *,即。$sql="SELECT `password` FROM `alumni` WHERE `username`='$user_check'";。然后,您可以不使用if($count==1),而是使用if(password_verify($old_pwd,$db_check->fetch()['password'])
标签: php mysqli change-password password-hash