laravel 5.4 csrf token mismatch

时间:2017-04-09 14:22:12

标签: laravel-5

  • Laravel版本:
Laravel Framework 5.4.17
  • PHP版本:
PHP 7.1.3-3+deb.sury.org~yakkety+1 (cli) (built: Mar 25 2017 14:01:32) ( NTS )Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies with Zend OPcache v7.1.3-3+deb.sury.org~yakkety+1, Copyright (c) 1999-2017,
     Zend Technologies

  • 数据库驱动程序&版本:
mysql  Ver 14.14 Distrib 5.7.17, for Linux (x86_64) using  EditLine wrapper

说明

我的日志不断填满:

Illuminate\Session\TokenMismatchException in /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:68

重现步骤:

附加说明:

来自Session.php

'encrypt' => true,
'http_only' => true,

来自.env

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_SECURE_COOKIE=true
QUEUE_DRIVER=sync

我的网站正在运行最新的NGINX,并且正在使用带有SSL Cert的HTTPS来加密。我按照这个指南。 https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

所以我的网站只能通过HTTPS访问。在我的浏览器中,我可以看到cookie被发送给我。 Imgur

我不知道为什么会这样。

更新: 所以我将令牌标签添加到我拥有的每个论坛中,并且仍然记录加载令牌不匹配错误。我浏览了所有观点,并添加了它们看起来像......

<!-- Add comment -->
<div class="col-md-12">
  {!! Form::open(array('route' => array('comment_torrent', 'slug' => $torrent->slug, 'id' => $torrent->id))) !!}
  {{ csrf_field() }}
  <div class="form-group">
    <label for="content">Your comment:</label><span class="badge-extra">Type <strong>colon :</strong> for emoji</span>
    <textarea name="content" cols="30" rows="5" class="form-control"></textarea>
  </div>
  <button type="submit" class="btn btn-danger">{!! trans('traduction.save') !!}</button><label class="checkbox-inline"><input type="checkbox" name="confirmation"><strong>Anonymous Comment</strong></label>
  {!! Form::close() !!}
</div>
<!-- /Add comment -->

1 个答案:

答案 0 :(得分:1)

You get this error when you don't pass CSRF token with your form. You can just add {{ csrf_field() }} in your forms and it will start working. For example:

<form method="POST" action="#>
    {{ csrf_field() }}
    // Your Fields here
</form>

Hope that helps! I am sure your error will be removed after adding {{ csrf_field() }} to your forms. It's in-built security feature from Laravel.

相关问题