我有个人资料表格,用户可以编辑自己的个人资料。以这种形式,我有当前密码。必须匹配才能保存到数据库中。
表格:
{{ 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
。
您使用了错误的参数顺序。它是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
Hash :: check()有两个参数,第一个是平面密码,另一个是哈希密码。如果密码与哈希匹配,它将返回true。
Hash::check(normal_password,hashed_password);
示例:
Hash::check('123456a','$2y$10$.XB30GO4jn7bx7EauLrWkugIaCNGxiQCgrFTeFDeSSrGdQYd6Rneq');
我遇到了同样的问题,并像这样解决了它:
我发现我在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);
}
}