gpt4 book ai didi

php - mysql 使用 laravel php 检查数据库中的用户名和密码是否匹配

转载 作者:行者123 更新时间:2023-11-29 18:55:14 26 4
gpt4 key购买 nike

当用户尝试登录但用户名和密码与表中不匹配时,我在身份验证方面遇到小问题,如何返回登录页面并显示错误消息

login.blade.php

<form class="login-box animated fadeInUp" action="valdateData" method="POST" >
{{csrf_field()}}

<div class="box-header">
<h2>Log In</h2>
</div>
<label for="username">Username</label>
<br/>
<input type="text" id="username" name="username">
<br/>
<label for="password">Password</label>
<br/>
<input type="password" id="password" name="password">
<br/>
<button type="submit">Sign In</button>
<br/>
<a href="#"><p class="small">Forgot your password?</p></a>
</form>

web.php

Route::post('/testgetvalue','OrdersController@GetValues');
Route::get('/ES','OrdersController@PrepareIndex');
Route::get('/loginForm','LoginController@ShowLoginPage');
Route::post('/valdateData','LoginController@checkValidate');
Route::post('login/{id}','LoginController@ShowErrorMassege');

LoginController.php

public function ShowLoginPage()
{
return view('/loginForm');
}

public function checkValidate(Request $request)
{

$username=$request->input('username');
$password=$request->input('password');
$isVald=true;

$checkValdate = \DB::table('authentications')
->where(['username'=>$username,'password'=>$password])
->get();

if(count($checkValdate) > 0)
{
$isVald=true;
session()->set('UserValidate','true');
session()->set('username',$username);
//$value=session()->get('test');
// echo "session "+$value;
return redirect('/es');
} else {
return redirect('/login/'.$isVald);
}
}

在这一部分

return redirect('/login/'.$isVald);

如何返回登录页面并显示错误消息

谢谢

最佳答案

 $validator = Validator::make($request->all(), [
'username'=>'required|min:3|max:30',
'password'=>'digits_between:1,5000',


]);

if ($validator->fails())
{
return redirect()->back()->with('error', sprintf('Server failed provided data validation.Please try again and follow the validation rules as instructed.'));
}

以下是如何执行此操作的示例,具体取决于您的验证规则。您还可以为每个错误定义带有 Costum 消息的部分,您还应该使用“use Illuminate\Support\Facades\Validator;”在你的 Controller 中

关于php - mysql 使用 laravel php 检查数据库中的用户名和密码是否匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44175815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com