比较确认密码和散列密码 | Laravel 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18425370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:20:08  来源:igfitidea点击:

comparing confirmation password against a hashed password | Laravel 4

phpmysqlhashlaravellaravel-4

提问by Lynx

I am trying to get the confirmation password to work against the password field in my form. I went through the Validator methods and they all seem to work perfectly. However, when trying to confirm the password I get an error message everytime that they must match..scratching my head I can only determine it's because they are being hashed before going through validation. I am not sure how to get past this as they need to be hash before being entered into the database. Any ideas?

我正在尝试获取确认密码以针对表单中的密码字段工作。我浏览了 Validator 方法,它们似乎都运行良好。但是,在尝试确认密码时,每次它们必须匹配时,我都会收到一条错误消息。我摸不着头脑,我只能确定这是因为它们在通过验证之前已被散列。我不确定如何解决这个问题,因为它们需要在输入数据库之前进行哈希处理。有任何想法吗?

getSignUp Controller

获取注册控制器

        public function getSignUp() {
            $userdata = array(
                'email' => Input::get('email'),
                'password' => Hash::make(Input::get('password')),
                'confirm_password' => Hash::make(Input::get('confirm_password')),
                'user_zip_code' => Input::get('user_zip_code')         
            );

            $rules = array(
                'email' => 'required|email|unique:users,email',
                'password' => 'required|min:5',
                'confirm_password' => 'required|same:password',
                'user_zip_code' => 'required'
            );

            $validation = Validator::make($userdata, $rules);

            if($validation->fails()){
                return Redirect::to('signup')->withErrors($validation)->withInput();
            } 

            $user = new User($userdata);
            $user->save();

            return Redirect::to('login');
    }

If anymore code is needed let me know. I just simply have the withErrors going to the blade template for the signup page

如果需要更多代码,请告诉我。我只是将 withErrors 转到注册页面的刀片模板

回答by Joseph Silber

Don't pass the hashed password to the validator. Hash it before you save it:

不要将散列的密码传递给验证器。在保存之前散列它:

public function getSignUp() {
    $userdata = array(
        'email' => Input::get('email'),
        'password' => Input::get('password'),
        'confirm_password' => Input::get('confirm_password'),
        'user_zip_code' => Input::get('user_zip_code')         
    );

    $rules = ...

    $validation = Validator::make($userdata, $rules);

    if($validation->fails()){
        return Redirect::to('signup')->withErrors($validation)->withInput();
    } 

    $userdata['password'] = Hash::make($userdata['password']);

    $user = new User($userdata);
    $user->save();

    return Redirect::to('login');
}