我如何用新密码检查我的旧密码 [英] how can I check my old password with new password

查看:94
本文介绍了我如何用新密码检查我的旧密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用bcrypt()函数存储用户密码.现在,如果用户想要更改他/她的密码,那么他/她将输入他的旧密码,而我将使用旧密码进行检查.

问题是每当我对用户输入的密码使用bcrypt函数时,都会显示一些新生成的密码.

例如:注册期间,用户使用111111密码注册.在更改密码期间,用户还输入了111111,但是两者都不同.

问:为什么显示不同.我在laravel 5.4中使用它.

解决方案

您可以使用 Hash :: check()对照数据库中的旧密码来检查旧密码

  if(哈希:: check($ oldPassword,$ passwordFromDb)){//这是一个匹配} 

因此,示例实现为:

  $ oldPassword = $ request-> input('old-password');$ newPassword = $ request-> input('new-password');$ hashedPassword = Auth :: user()->密码;如果(Hash :: check($ oldPassword,$ hashedPassword)){$ user = User :: find(Auth :: user()-> id)->更新(['password'=>Hash :: make($ newPassword)]);} 

I am using bcrypt() function for storing my password of a user. Now if a user wants to change his/her password, then he/she will put his old password and I will check that with old password.

Problem is whenever I am using bcrypt function to the user inputed password it shows some new generated password.

Ex: During registration a user registered with 111111 password. And during change password the user also inputing 111111 but the both comes different.

Q: Why it shows different. I am using this in laravel 5.4.

解决方案

You can use Hash::check() to check the old password against the one you have in your database as such

if (Hash::check($oldPassword, $passwordFromDb)) 
{
   // it is a match
}

As such, an example implementation would be:

$oldPassword    = $request->input('old-password');
$newPassword    = $request->input('new-password');
$hashedPassword = Auth::user()->password;

if (Hash::check($oldPassword, $hashedPassword)) 
{
    $user = User::find(Auth::user()->id)
                ->update(
                    ['password'=> Hash::make($newPassword)]
                );
}

这篇关于我如何用新密码检查我的旧密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆