在Laravel中检查密码是否正确 [英] Check whether password is correct or not in Laravel

查看:957
本文介绍了在Laravel中检查密码是否正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在laravel中,我想检查用户是否输入当前密码,而不是使用存储在该用户数据中数据库中的密码进行检查.如果正确,则继续操作,否则输入密码错误.

In laravel I want to check if user enter current password than check with that password which is store in database in that user data. If correct than continue otherwise give message Password is incorrect.

我是laravel的新手,所以对此没有确切的想法.

I am new in laravel so not getting exact idea for this.

提前谢谢.

$('#pwd').blur(function(){
            var oldpwd=$('#pwd').val();
            var uid = $('#id').val();
            console.log(uid);
            if(oldpwd != "")
            {
              $.ajax({
                  url : "{{ url/profile/checkOldPwd}}",
                  data : { oldpwd:oldpwd , uid:uid },
                  type : "POST",
                  success : function(data) {
                    if(data == 0){
                      $("#msg-old").show();
                      $("#msg-old").html("Password is Incorrect!");
                      $("#pwd").val("");
                      $("#pwd").focus();
                  }
                  else
                  {
                    $("#msg-old").hide();
                    $("#msg-old").html("");
                  }
                }
                });
              }
            });

推荐答案

正如Hiren所提到的,您可以使用默认的已注册哈希器,该默认哈希器将传递给所使用的特定UserProvider.默认值为Illuminate\Hashing\BcryptHasher.

As Hiren has mentioned you can use the default registered hasher as that is passed to the specific UserProvider used. The default is Illuminate\Hashing\BcryptHasher.

您可以通过多种方式使用它:

You can use it a couple of ways:

  1. 从容器中取出

$user = User::find($id);
$hasher = app('hash');
if ($hasher->check('passwordToCheck', $user->password)) {
    // Success
}

  1. 使用立面

$user = User::find($id);
if (Hash::check('passwordToCheck', $user->password)) {
    // Success
}

  1. 使用通用php函数password_verify失去兴趣也可以.但这是可行的,因为它使用的默认哈希算法是bcrypt.
  1. Out of interest using the generic php function password_verify also works. However that works because the default hashing algorithm it uses is bcrypt.

if (password_verify('passwordToCheck', $user->password)) {
    // Success
}

这篇关于在Laravel中检查密码是否正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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