2017-05-25 316 views
0

我有小問題與身份驗證時,用戶嘗試登錄,但在表中沒有匹配的用戶名和密碼,如何才能錯誤返回按摩MySQL的檢查,如果用戶名和密碼使用laravel PHP

登錄到登錄頁面的數據庫匹配.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','[email protected]'); 
Route::get('/ES','[email protected]'); 
Route::get('/loginForm','[email protected]'); 
Route::post('/valdateData','[email protected]'); 
Route::post('login/{id}','[email protected]'); 

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

如何錯誤返回消息到登錄頁面

感謝

+0

你正在使用php artisan make:auth? –

+0

不,我使用自定義表格 –

+1

你可以使用'with()'函數。 'return redirect('/ login /'.$ isVald) - > with('status','錯誤信息!');'然後你將在你的會話中擁有'status'。 – Adam

回答

0
$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.')); 
    } 

下面是如何例你可以做到這一點,取決於你的確認規則。您也可以使用foreach錯誤消息來定義partials,您還應該使用「使用Illuminate \ Support \ Facades \ Validator;」在你的控制器中