php “由于不活动,页面已过期” - Laravel 5.5

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

"The page has expired due to inactivity" - Laravel 5.5

phplaravelcsrflaravel-5.5

提问by Sougata Bose

My register page is showing the form properly with CsrfToken ({{ csrf_field() }}) present in the form).

我的注册页面正确显示了表格,表格中{{ csrf_field() }}存在CsrfToken ( )。

Form HTML

表单 HTML

<form class="form-horizontal registration-form" novalidate method="POST" action="{{ route('register') }}">
        {{ csrf_field() }}
        ....
</form>

I am using inbuilt authentication for the users. Have not changed anything except the routes and redirects.

我正在为用户使用内置身份验证。除了路由和重定向之外没有改变任何东西。

When I submit the form (just after reloading also), it gives that The page has expired due to inactivity. Please refresh and try again.error.

当我提交表单时(也在重新加载之后),它表明页面由于不活动而过期。请刷新并重试。错误。

My be I am missing a very small thing. But not sure what it is. Any help?

我是我错过了一件非常小的事情。但不确定它是什么。有什么帮助吗?

Update

更新

Found the issue. The session driver was set to array. Changed it to file and the error is gone now. But what is wrong if I use array?

发现问题。会话驱动程序设置为数组。将其更改为文件,现在错误消失了。但是如果我使用数组有什么问题呢?

回答by Devon

If you're coming to this answer directly from a search, make sure you have already added the csrf token to your form with {{ csrf_field() }}like the OP.

如果您直接从搜索中获得此答案,请确保您已经{{ csrf_field() }}像 OP 一样将csrf 令牌添加到您的表单中。



If you have your session driver set to file:

如果您将会话驱动程序设置为文件:

May have something to do with the storage_path not being writable. This is where it stores session data regarding tokens if you're using file based sessions. The can be verified with is_writable(config('session.files'))

可能与 storage_path 不可写有关。如果您使用基于文件的会话,它就是存储有关令牌的会话数据的地方。可以通过以下方式验证is_writable(config('session.files'))



For the OP, the session driver was set to array.Array is for testing only. Since data is not persisted, it will not be able to compare the token on the next request.

对于 OP,会话驱动程序设置为数组。数组仅用于测试。由于数据未持久化,因此无法在下一个请求中比较令牌。

The array driver is used during testing and prevents the data stored in the session from being persisted.

阵列驱动程序在测试期间使用并防止会话中存储的数据被持久化。

https://laravel.com/docs/5.5/session#configuration

https://laravel.com/docs/5.5/session#configuration



Check config/session.php

检查配置/session.php

Lastly, an issue I just had, we had a project which has the session domain and secure settings in config/session.php but the development site was not using HTTPS (SSL/TLS). This caused this generic error since sessions.secure was set to true by default.

最后,我刚刚遇到的一个问题,我们有一个项目,它在 config/session.php 中有会话域和安全设置,但开发站点没有使用 HTTPS (SSL/TLS)。这导致了这个通用错误,因为 session.secure 默认设置为 true。

回答by Hyder B.

I ran into the same issue in Laravel 5.5. In my case, it happened after changing a route from GET to POST. The issue was because I forgot to pass a CSRF token when I switched to POST.

我在 Laravel 5.5 中遇到了同样的问题。就我而言,它发生在将路由从 GET 更改为 POST 之后。问题是因为我在切换到 POST 时忘记传递 CSRF 令牌。

You can either post a CSRF token in your form by calling:

您可以通过调用以下方式在表单中发布 CSRF 令牌:

 {{ csrf_field() }}

Or exclude your route in app/Http/Middleware/VerifyCsrfToken.php

或者在 app/Http/Middleware/VerifyCsrfToken.php 中排除你的路由

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

回答by Sagar Chamling

Try all of them.

尝试所有这些。

composer dump-autoload
php artisan optimize
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear

回答by yuklia

This caused because of Illuminate\Session\TokenMismatchExceptionlook at this code sample how to handle it properly:

这是因为Illuminate\Session\TokenMismatchException查看此代码示例如何正确处理它:

https://gist.github.com/jrmadsen67/bd0f9ad0ef1ed6bb594e

https://gist.github.com/jrmadsen67/bd0f9ad0ef1ed6bb594e

回答by Andrés Ruiz

My case was solved with SESSION_DOMAIN, in my local machine had to be set to xxx.localhost. It was causing conflicts with the production SESSION_DOMAIN, xxx.comthat was set directly in the session.php config file.

我的情况解决了SESSION_DOMAIN,在我的本地机器上必须设置为xxx.localhost. 它可能会导致与生产发生冲突SESSION_DOMAINxxx.com这是直接设置在session.php文件的配置文件。

回答by andrew_Hymanson

For those who still has problem and nothing helped. Pay attention on php.ini mbstring.func_overload parameter. It has to be set to 0. And mbstring.internal_encoding set to UTF-8. In my case that was a problem.

对于那些仍然有问题并且没有任何帮助的人。注意 php.ini mbstring.func_overload 参数。它必须设置为 0。并且 mbstring.internal_encoding 设置为 UTF-8。就我而言,这是一个问题。

回答by Suresh Velusamy

Some information is stored in the cookie which is related to previous versions of laravel in development. So it's conflicting with csrf generated tokens which are generated by another's versions. Just Clear the cookie and give a try.

一些信息存储在 cookie 中,这些信息与开发中的 laravel 以前的版本相关。所以它与由其他版本生成的 csrf 生成的令牌相冲突。只需清除cookie并尝试一下。

回答by Buddhi Kasun

I change permission to storage and error was gone. It seemed lack of permission was the issue.

我更改了存储权限,错误消失了。似乎缺乏许可是问题所在。

sudo chmod -R 775 storage/

回答by Shery

add @csrfin the form and also go to VerifyCsrfToken.php

添加@csrf表单并转到VerifyCsrfToken.php

app->Http->Middleware->VerifyCsrfToken.php

app->Http->Middleware->VerifyCsrfToken.php

protected $except = [
        'paste your route here'
    ];

回答by Irfandi D. Vendy

In my case, the site was fine in server but not in local. Then I remember I was working on secure website.
So in file config.session.php, set the variable secure to false

就我而言,该网站在服务器上很好,但在本地则不然。然后我记得我正在开发安全网站。
因此,在文件 config.session.php 中,将变量 secure 设置为 false

'secure' => env('SESSION_SECURE_COOKIE', false),