某些设备上的 VerifyCsrfToken.php 第 68 行中的 Laravel 5.4 TokenMismatchException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43156466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:42:08  来源:igfitidea点击:

Laravel 5.4 TokenMismatchException in VerifyCsrfToken.php line 68 on some device

phplaravellaravel-5php-7

提问by myh34d

I know it's one of the most asked question around here, but I really read everything on the first 10 pages results on google and nothing solved my problem.

我知道这是这里问得最多的问题之一,但我真的阅读了 google 前 10 页结果中的所有内容,但没有解决我的问题。

On a fresh installationof laravel 5.4 , generated the auth controller, views, etc. via php artisan make:authwhen I try to register (or login) in some deviceI get the following error:

全新安装的 laravel 5.4 上,通过php artisan make:auth生成身份验证控制器、视图等,当我尝试在某些设备中注册(或登录)时,出现以下错误:

TokenMismatchException in VerifyCsrfToken.php line 68

VerifyCsrfToken.php 第 68 行中的 TokenMismatchException

I'm running this application in local on debian 8.7 and php7 (could it be related to the problem in some way ?) From my pc, iphone 7, another windows 10 desktop it works perfectly, but on a Huawei p8 lite, ipad 2 and lubuntu (quite old version) I get that error. So it's really weird because it just happends with some device (no matter whaht O.S. no matter what browser).

我在 debian 8.7 和 php7 的本地运行这个应用程序(它可能以某种方式与问题有关?)从我的电脑,iphone 7,另一个 Windows 10 桌面它运行完美,但在华为 p8 lite,ipad 2和 lubuntu(相当旧的版本)我得到了那个错误。所以这真的很奇怪,因为它只是在某些设备上发生(不管是什么操作系统,不管是什么浏览器)。

I can already tell you that I tried grant permissions to sessions folder, change session_domain, clearing cache (but as I said I'm trying this on a fresh installation), change {{ csrf_token }} to {!! csrf_token !!} , etc.

我已经可以告诉你,我尝试授予 session 文件夹的权限,更改 session_domain,清除缓存(但正如我所说,我正在全新安装中尝试此操作),将 {{ csrf_token }} 更改为 {!! csrf_token !!} 等

Just to give you more information about my system, this is the php version i'm using:

只是为了给你更多关于我的系统的信息,这是我正在使用的 php 版本:

php --version
PHP 7.0.17-1~dotdeb+8.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.17-1~dotdeb+8.1, Copyright (c) 1999-2017, by Zend 
Technologies

I'm really stuck on this for days. Got no more ideas.

我真的坚持了好几天。没有更多的想法。

回答by Haninder Singh

I'm assuming you added $this->middleware('auth'); inside the constructor of your controller to get the authentication working. Add the following at the top as well, under your login/register forms, if you are using {!! Form::someElement !!}:

我假设你添加了 $this->middleware('auth'); 在您的控制器的构造函数中进行身份验证。如果您使用的是 {!! 表单::someElement !!}:

{!! csrf_field() !!}

Or if you are using input tags inside your forms, just add this just after the <form>tag:

或者,如果您在表单中使用输入标签,只需在<form>标签后添加:

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

回答by Moode Osman

in your .envfile add

在您的.env文件中添加

SESSION_DOMAIN=yourdomain.com

回答by Hasini

I had the same issue. I did these steps.

我遇到过同样的问题。我做了这些步骤。

  1. php artisan cache:clear //cache clear
  2. <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> //place inside the form tags
  1. php artisan cache:clear //cache clear
  2. <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> //place inside the form tags