【问题标题】:mysql check if username and password matches in database using laravel phpmysql使用laravel php检查数据库中的用户名和密码是否匹配
【发布时间】:2017-05-25 17:11:47
【问题描述】:

当用户尝试登录但用户名和密码在表中不匹配时,我有一个小问题,如何返回登录页面并显示错误消息

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);

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

谢谢

【问题讨论】:

  • 你正在使用 php artisan make:auth?
  • 不,我正在使用自定义表格
  • 您可以使用with() 函数。 return redirect('/login/'.$isVald)-&gt;with('status', 'Error message!'); 然后您的会话中将有 status

标签: php mysql laravel


【解决方案1】:
 $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 消息的部分,您还应该使用“使用 Illuminate\Support\Facades\Validator;”在你的控制器中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-02
    • 2018-05-12
    • 2014-12-01
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    相关资源
    最近更新 更多