【问题标题】:Laravel Password Hash ValidationLaravel 密码哈希验证
【发布时间】:2019-12-14 12:58:52
【问题描述】:

我在 Laravel 6.X 中编写了自定义用户系统。在我的登录控制器中,我有这个验证码:

    $email = $request->email;
    $validator = Validator::make($request->all(), [
        'email' => 'bail|email|required|max:32|exists:users,email',
        'password' => ['required','min:1','max:32',
        function ($attribute, $value, $fail) {
            $users = DB::table('users')->where('email', $email)->get(); // Get user info (Exception is in here)
            if (hash('sha256', $value) !== $users[0]->Password) { // Check if hashed password equals
                $fail($attribute.' is wrong.'); // If not equal then return error message
            }
        },
    ],
    ]);

我使用 SHA256 哈希存储我的密码。如果密码不等于数据库密码,则向用户返回错误消息。但我得到一个例外“未定义的变量:电子邮件”。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    查看此答案:https://stackoverflow.com/a/11086796/7897328。我认为您需要使用use 关键字。

    您的代码:

    function ($attribute, $value, $fail) {
    

    您的代码带有use 关键字:

    function ($attribute, $value, $fail) use ($email) {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多