Laravel 5.1 中 VerifyCsrfToken.php 第 53 行中的 TokenMis
2022-11-25
php問題
html5模板網
TokenMismatchException in VerifyCsrfToken.php line 53 in Laravel 5.1(Laravel 5.1 中 VerifyCsrfToken.php 第 53 行中的 TokenMismatchException)
本文介紹了Laravel 5.1 中 VerifyCsrfToken.php 第 53 行中的 TokenMismatchException的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
當我嘗試登錄時顯示令牌錯誤.我在視圖中檢查了令牌是正確的,當評論 AppHttpMiddlewareVerifyCsrfToken::class
時,在 Kernel.php
它讓我登錄,但重定向到我的儀表板后我沒有登錄.我在 mac 上使用 MAMP.
<h1>登錄</h1><div>{!!Form::open(['url'=>'user/login','class' =>'']) !!}<input type="hidden" name="_token" value="{{ csrf_token() }}"><ul><li><label>客戶代碼</label>{!!Form::Text('customer_code',Input::old('customer_code'),['class'=>''])!!}<li><label>密碼</label>{!!Form::Password('password','',['class'=>''])!!}</li><li>{!!Form::submit('Submit',array('class' => 'btn')) !!}</li>{!!表格::關閉()!!}
<div><a href="{!!URL::to('user/forget_password')!!}">忘記密碼</a></div>
同時我使用Sentry Package
登錄.
/*** 登錄后*/公共函數 post_login(){嘗試{$規則= ['customer_code' =>'必需的','密碼' =>'必需的',];$消息 = ['customer_code.required' =>'錯誤1','password.required' =>'error2'];$validator = Validator::make(Input::all(), $rules,$message);如果 ($validator->fails()){return Redirect::back()->withErrors($validator)->withInput();}//如果 ($validator->fails())別的{$authUser = Sentry::authenticateAndRemember(array('customer_code' =>Input::get('customer_code'),'密碼' =>Input::get('password')), false);如果($ authUser){//$login = Sentry::loginAndRemember($authUser);return Redirect::to('user/panel/'.$authUser->id)->with('comment', 'Welcome');}別的{return Redirect::back()->with('comment', '登錄錯誤');}}//驗證器}catch(Exception $e){return Redirect::back()->withInput(Input::except('password','file'))->withErrors(['ERROR!!!!!']);}}
解決方案
已
由于您使用的是表單構建器,因此請從表單中刪除它.當您執行 Form::open()
時,Laravel 表單構建器會自動向您的表單添加一個隱藏的令牌字段
所以刪除這一行:
When I try to login show me token error. I have checked token in view form it's right and when comment AppHttpMiddlewareVerifyCsrfToken::class
,
in the Kernel.php
it makes me login but after Redirect to my dashboard I'm not logged in. I am using MAMP on mac.
<div>
<h1>Login</h1>
<div>
{!! Form::open(['url'=>'user/login','class' => '']) !!}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<ul>
<li><label>Customer Code</label>{!!Form::Text('customer_code',Input::old('customer_code'),['class'=>''])!!}</li>
<li><label>Password</label>{!!Form::Password('password','',['class'=>''])!!}</li>
<li>{!! Form::submit('Submit',array('class' => 'btn')) !!}</li>
</ul>
{!!Form::close()!!}
</div>
<div><a href="{!!URL::to('user/forget_password')!!}">Forget Password</a></div>
</div>
Meanwhile I use Sentry Package
for login.
/**
* post_login
*/
public function post_login()
{
try
{
$rules = [
'customer_code' => 'required',
'password' => 'required',
] ;
$message = [
'customer_code.required' => 'erorrr1',
'password.required' =>'error2'
];
$validator = Validator::make(Input::all(), $rules,$message);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
} // if ($validator->fails())
else
{
$authUser = Sentry::authenticateAndRemember(array(
'customer_code' => Input::get('customer_code'),
'password' => Input::get('password')), false);
if($authUser)
{
//$login = Sentry::loginAndRemember($authUser);
return Redirect::to('user/panel/'.$authUser->id)->with('comment', 'Welcome');
}
else
{
return Redirect::back()->with('comment', 'Error for login');
}
}//validator
}
catch(Exception $e)
{
return Redirect::back()->withInput(Input::except('password','file'))->withErrors(['ERROR!!!!!']);
}
}
解決方案
Edited:
Since you are using Form builder remove this from your form. Laravel form builder automatically adds a hidden token field to your form when you do Form::open()
So remove this line:
<input type="hidden" name="_token" value="{{ csrf_token() }}">
這篇關于Laravel 5.1 中 VerifyCsrfToken.php 第 53 行中的 TokenMismatchException的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!