如何在laravel中获取加密密码值?

时间:2014-04-07 10:34:39

标签: php laravel

我正在尝试更改用户的密码,我需要检查该用户的旧密码是否与我从html“oldpass”文本框中获取的值匹配。如果现有密码值和“oldpass”值匹配,则新密码将在数据库中更新。我从数据库获取的密码值已加密。

$ userpass = User :: where('id','=',Session :: get('userid')) - > get(array('password')); 问题是$ userpass返回一个空值。

以下是代码:

    $oldpass = Hash::make(Input::get('oldpass'));//Getting password from html form
    $userpass = User::where('id', '=', Session::get('userid'))->get(array('password'));//Getting password value from database Users table
    if ($oldpass === $userpass) {

         User::where('id', '=', Session::get('userid'))
            ->update(array(
                'password' => Hash::make(Input::get('newpass'))
    ));

    } else {
        Return View::make('changepass.changepass')
                        ->with('errormessage', 'Password does not match');
    }

1 个答案:

答案 0 :(得分:1)

这里有两个主要问题。

  1. 一方面,$userpass返回null,因为get()不是fecth列的适当函数。您可以使用pluck(参见query builder docs

    无论如何,只要您获取用户,就可以调用该属性:

    $userpass = User::find(Session::get('userid'))->password;

  2. 您正在尝试将散列密码与普通密码进行比较。 Laravel使用Guard 默认情况下,管理用户身份验证和Guard使用Hash::make来存储它。您应该将哈希值与:

    进行比较

    Hash::check($oldpass, $userpass)

  3. 您也可以使用Guard检查用户凭据是否正确Auth::validate($credentials)(请参阅Laravel Security),然后更改密码,如:

    if(Auth::validate('id' => Session::get('userid'), 'password' => Input::get('oldpass'))){
      //Assuming user was authenticated before. If not use Auth::attempt instead of validate
      Auth::user()->password = Hash::make(Input::get('newpass')); 
    } else {
        Return View::make('changepass.changepass')
                        ->with('errormessage', 'Password does not match');
    }
    
相关问题