Laravel Hash :: check()始终返回false

问题描述 投票:15回答:5

我有个人资料表格,用户可以编辑自己的个人资料。以这种形式,我有当前密码。必须匹配才能保存到数据库中。

表格:

{{ Form::password('currPassword', array('id'=>'currPassword')) }}

我想在Controller中具有此功能以通过数据库进行检查。

$data = User::find($id);
if( ! Hash::check( $data->password , Input::get('currPassword') ) )
{
    return Redirect::to('/admin/profile')
        ->with('message', 'Current Password Error !')
        ->withInput();
}

123456密码输入数据库是可以的,将123456放入currPassword后必须返回TRUE,但始终返回FALSE

php laravel laravel-4
5个回答
41
投票

您使用了错误的参数顺序。它是Hash::check($input, $hash),而不是相反。

简短修补示例:

[1] > $pw = 123456;
// 123456
[2] > $hashed = Hash::make($pw);
// '$2y$10$xSugoyKv765TY8DsERJ2/.mPIOwLNdM5Iw1n3x1XNVymBlHNG4cX6'
[3] > Hash::check($hashed, $pw);
// false
[4] > Hash::check($pw, $hashed);
// true

4
投票

Hash :: check()有两个参数,第一个是平面密码,另一个是哈希密码。如果密码与哈希匹配,它将返回true。

Hash::check(normal_password,hashed_password);

示例:

Hash::check('123456a','$2y$10$.XB30GO4jn7bx7EauLrWkugIaCNGxiQCgrFTeFDeSSrGdQYd6Rneq');

3
投票

我遇到了同样的问题,并像这样解决了它:

我发现我在RegistrationService类中使用了Hash :: make函数,更重要的是,我<setPasswordAttribute函数在我的[[User model中迅速忘记了: class User extends Model implements AuthenticatableContract, AuthorizableContract { ... /** * @param $value */ public function setPasswordAttribute($value) { $this->attributes['password'] = Hash::make($value); } }

因此,密码是经过两次哈希处理的,当然,每个Hash :: check调用都不正确并返回false。

2
投票

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.