php Laravel 5.5 登录和注册页面说:由于不活动,页面已过期。[TokenMismatchException]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45994235/
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-08-26 03:10:33  来源:igfitidea点击:

Laravel 5.5 login and register page says:The page has expired due to inactivity.[TokenMismatchException]

phplaravellaravel-5.5

提问by brian.shen

I just created a new project of laravel version 5.5 with the laravel installer.And run the command "php artisan make:auth".The views and controller are generated for the user authentication.And also run "php artisan migrate" to create tables needed in the database.When visiting the login page and register page,filling the form and submit.It shows "The page has expired due to inactivity. Please refresh and try again.".But refreshing the page does nothing help. Seen in the source code,where causes the exception:

我刚刚使用laravel安装程序创建了一个laravel 5.5版的新项目。并运行命令“ php artisan make:auth”。为用户身份验证生成视图和控制器。同时运行“ php artisan migrate”以创建所需的表在数据库中。访问登录页面和注册页面时,填写表单并提交。它显示“由于不活动,页面已过期。请刷新并重试。”。但刷新页面没有任何帮助。在源代码中看到,导致异常的地方:

if ($e instanceof ModelNotFoundException) {
        $e = new NotFoundHttpException($e->getMessage(), $e);
    } elseif ($e instanceof AuthorizationException) {
        $e = new AccessDeniedHttpException($e->getMessage());
    } elseif ($e instanceof TokenMismatchException) {
        $e = new HttpException(419, $e->getMessage());
    }

It seems "TokenMismatchException" causing this problem. When did this happen?And why?I just create this new project and did not do any other changes. Hope you got the points. I use php 7.1.9(laravel 5.5 requires php > 7.0.0).And serve the project in develop environment with:php artisan serve

似乎“TokenMismatchException”导致了这个问题。这是什么时候发生的?为什么?我只是创建了这个新项目,没有做任何其他更改。希望你得到积分。我使用 php 7.1.9(laravel 5.5 需要 php > 7.0.0)。并在开发环境中为项目提供服务:php artisan serve

采纳答案by studiojasper_pl

I had the same problem on localhost:8000(php artisan serve). Maybe it's coincidence, but try on "clean browser" , other than you used with previous development. For me it worked.

我在localhost:8000( php artisan serve)上遇到了同样的问题。也许是巧合,但请尝试使用“干净的浏览器”,而不是您在以前的开发中使用过的。对我来说它奏效了。

It seems that the problem is with cookies from development with previous Laravel versions, on the same url.

似乎问题出在同一 url 上使用以前的 Laravel 版本开发的 cookie。

回答by Kokil

i think you missed csrf token.

我想你错过了 csrf 令牌。

dont forget to use {!! csrf_field() !!}

不要忘记使用 {!! csrf_field() !!}

回答by Maniruzzaman Akash

In my case, I've got the same error message and then figured that I missed to add csrf_token

就我而言,我收到了相同的错误消息,然后认为我错过了添加 csrf_token

{{ csrf_field() }}

Or without form helper that will be,

或者没有表单助手,

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

If that doesn't work, then-

如果那不起作用,那么-

Refresh the browser cacheand hopefully it will work, thanks.

刷新浏览器缓存,希望它会起作用,谢谢。

回答by Afraz Ahmad

I had the same issue and it was because I was using virtualhost and I setup below variable to mydomain.com. in config/session.php file

我遇到了同样的问题,这是因为我使用的是虚拟主机,并且我将下面的变量设置为 mydomain.com。在 config/session.php 文件中

'domain' => env('SESSION_DOMAIN', 'mydomain.com'),

When I changed it to null then it started working

当我将其更改为 null 然后它开始工作

'domain' => env('SESSION_DOMAIN', 'null'),

I don't know what is the reason behind it but it is working fine now.

我不知道它背后的原因是什么,但它现在工作正常。

回答by shalini

Add csrf token

添加 csrf 令牌

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

回答by Dylan Glockler

It appears to be permission settings on either storage and/or bootstrap/cache.

它似乎是存储和/或引导程序/缓存的权限设置。

I'm using a Cloudways server. I reset permissions on my server under Application Settings and it now works. On my local development server the equivalent was to set chmod 777 on storage.. I had used 755 previously and the error persisted.

我正在使用 Cloudways 服务器。我在应用程序设置下重置了我的服务器的权限,现在它可以工作了。在我的本地开发服务器上,相当于在存储上设置 chmod 777 .. 我以前使用过 755 并且错误仍然存​​在。

回答by mark edosa

I had this same issue. vagrant reload --provisionworked for me

我有同样的问题。 vagrant reload --provision为我工作

回答by Raza Mehdi

I would also keep csrf_tokenin a meta tag.

我也会保留csrf_token一个元标记。

<meta name="csrf-token" content="{{ csrf_token() }}">

回答by Allan Mwesigwa

I was only testing post requests for my api and came across the same issue. I resolved it by adding my route to the Middleware VerifyCsrfToken's $exceptarray i.e. go to app/Http/Middleware/VerifyCsrfTokenand add

我只是在测试我的 api 的发布请求并遇到了同样的问题。我通过将我的路由添加到 MiddlewareVerifyCsrfToken$except数组来解决它,即转到app/Http/Middleware/VerifyCsrfToken并添加

protected $except = [
        'your/route'
];

But if your requests are from some front-end platform or views, it's advisable to add {{ csrf_field() }}in the form that sends the request.

但是如果你的请求来自某个前端平台或视图,建议{{ csrf_field() }}在发送请求的表单中添加。

回答by Ali Najafi

Hi for the group paths that you want to apply to everyone, use this method, which is my 5.5 larval version. Use the star => go to app/Http/Middleware/VerifyCsrfToken and add

嗨,对于您要应用于所有人的组路径,请使用此方法,这是我的 5.5 幼虫版本。使用星号 => 转到 app/Http/Middleware/VerifyCsrfToken 并添加

protected $except = [
    '/user/*'
];

This is also my user's path

这也是我用户的路径

Route::group(['prefix' => 'user', 'namespace' => 'User', 'as' => 'user.'] , function (){