php - Laravel 5.1 中 VerifyCsrfToken.php 第 53 行中的 TokenMismatchException

标签 php session laravel-5 session-cookies csrf

当我尝试登录时显示 token 错误。我已经检查了 View 形式的 token 是正确的,当评论 \App\Http\Middleware\VerifyCsrfToken::class 时, 在 Kernel.php 它让我登录,但在重定向到我的仪表板后我没有登录。我在 mac 上使用 MAMP。

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

同时我使用 Sentry Package 进行登录。

    /**
     * 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!!!!!']);
         }
}

最佳答案

编辑:

由于您使用的是表单生成器,因此请将其从您的表单中删除。当你执行 Form::open()

时,Laravel 表单构建器会自动将一个隐藏的标记字段添加到你的表单中

所以删除这一行:

 <input type="hidden" name="_token" value="{{ csrf_token() }}">

关于php - Laravel 5.1 中 VerifyCsrfToken.php 第 53 行中的 TokenMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30934906/

相关文章:

php - 通过电子邮件发送动态生成的调查页面的内容

java - 使用 Apache CXF DOSGi 进行身份验证和 session 管理

java - Hibernate - Log4j - 跟踪错误

laravel - 使用 $append 属性进行 Eloquent Eager Loading

php - 解决方案 "Could not find driver"& "can' t 通过套接字连接到本地 MYSQL 服务器” - Laravel

mysql - Laravel 跨不同数据库的关系

php - 如何在一个 Eloquent 查询中增加和更新列

php - MYSQL 和 PHP 对结果进行分组

php - Mysql中如何防止负数

asp.net - Context.User.Identity.Name 与 HttpContext.Current.User.Identity.Name