phpmyadmin 正在检查数据库中密码列的旧密码

q5lcpyga  于 2023-01-13  发布在  PHP
关注(0)|答案(2)|浏览(178)

我需要检查用户输入的密码,如果正确,我将用新密码更新密码,如果密码错误,我希望在laravel 5.4中给用户一个警告。
控制器:

public function edit(Request $request){

    $user = new User;
    $user = $user->find(1);

            $oldpassword = $request->input('oldpassword');
            $newpassword = $request->input('newpassword');

        //if condition 
        /*if (user()->id == $oldpassword){
            $updatesucess = 1;
        }else {
            $updatesucess = 0;
        }*/

            $user->update(['password' => $newpassword]);
            return back();
        }

查看:

@section('content')
<div class="form">
    <div class="logo-area">
        <img src="images/logo.png">
    </div>
    <form method="POST" action="/edit">
     {{ csrf_field() }}
        <input type="password" name="oldpassword" placeholder="old Password" required>
        <input type="password" name="newpassword" placeholder="new Password" required>
        <input type="submit" value="Save changes">
    </form>
8fsztsew

8fsztsew1#

使用Hash外观(documentation)的check方法
1.在您的控制器file中添加哈希外观:

use Illuminate\Support\Facades\Hash;

1.添加密码检查,如果不匹配,则使用错误处理重定向

$user = User::find(1);

$oldpassword = $request->input('oldpassword');

if (Hash::check($oldpassword, $user->password)) {
  // redirect back with error
}

1.使用Hash::make更新用户密码

$newpassword = $request->input('newpassword'); 
 $user->update(['password' => Hash::make($newpassword)]);
mqkwyuun

mqkwyuun2#

对于Laravel版本〉8,这是最佳解决方案。

'old_password' => 'required|current_password'

您还可以指定API

'old_password' => 'required|current_password:web'

相关问题