在 Laravel 中发布请求 - 错误 - 419 抱歉,您的会话/419 您的页面已过期 [英] Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired

查看:32
本文介绍了在 Laravel 中发布请求 - 错误 - 419 抱歉,您的会话/419 您的页面已过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了 Laravel 5.7

I installed Laravel 5.7

在文件中添加了一个表单 esourcesviewswelcome.blade.php

Added a form to the file esourcesviewswelcome.blade.php

<form method="POST" action="/foo" >
    @csrf
    <input type="text" name="name"/><br/>
    <input type="submit" value="Add"/>
</form>

添加到文件 outesweb.php

Route::post('/foo', function () {
    echo 1;
    return;
});

发送 POST 请求后:

After sending a POST request:

419 抱歉,您的会话已过期.请刷新并重试.

419 Sorry, your session has expired. Please refresh and try again.

5.6 版本没有这个问题.

推荐答案

在阅读以下内容之前,请确保您有 @csrf{{ csrf_field() }} 在您的表单中喜欢

Before reading below make sure you have @csrf or {{ csrf_field() }} in your form like

<form method="post">
@csrf <!-- {{ csrf_field() }} -->
... rest of form ...
</form>

Laravel 中出现 Session Expired 或 419 Page Expired 错误消息是因为您的 csrf 令牌验证在某处失败,这意味着 AppHttpMiddlewareVerifyCsrfToken::class 中间件已经打开.在表单中已经添加了 @csrf 刀片指令,这也应该没问题.

The Session Expired or 419 Page Expired error message in Laravel comes up because somewhere your csrf token verification fails which means the AppHttpMiddlewareVerifyCsrfToken::class middleware is already turned on. In the form the @csrf blade directive is already added, which should be fine as well.

然后要检查的另一个区域是会话.csrf 令牌验证直接涉及您的会话,因此您可能需要检查您的会话驱动程序是否正常工作,例如配置不正确的 Redis 可能会导致问题.

Then the other area to check is the session. The csrf token verification is directly involved with your session, So you might want to check whether your session driver is working or not, such as an incorrectly configured Redis might cause an issue.

也许您可以尝试从 .env 文件切换会话驱动程序/软件,下面给出了支持的驱动程序

Maybe you can try switching your session driver/software from your .env file, the supported drivers are given below

Laravel 5、Laravel 6 和 Laravel 7 中支持的会话驱动程序 (文档链接)

  • file - 会话存储在 storage/framework/sessions 中.
  • cookie - 会话存储在安全的加密 cookie 中.
  • database - 会话存储在关系数据库中.
  • memcached/redis - 会话存储在这些基于缓存的快速存储之一中.
  • array - 会话存储在 PHP 数组中,不会被持久化.
  • file - sessions are stored in storage/framework/sessions.
  • cookie - sessions are stored in secure, encrypted cookies.
  • database - sessions are stored in a relational database.
  • memcached / redis - sessions are stored in one of these fast, cache based stores.
  • array - sessions are stored in a PHP array and will not be persisted.

如果您的表单在切换会话驱动程序后工作,那么该特定驱动程序有问题,请尝试从那里修复错误.

If your form works after switching the session driver, then something wrong is with that particular driver, try to fix the error from there.

可能容易出错的场景

  • 可能基于文件的会话可能无法工作,因为 /storage 目录的权限问题(快速谷歌搜索将找到解决方案),还记得为目录放置 777从来没有解决方案.

  • Probably file-based sessions might not work because of the permission issues with the /storage directory (a quick googling will fetch you the solution), also remember putting 777 for the directory is never the solution.

在数据库驱动的情况下,你的数据库连接可能是错误的,或者sessions表可能不存在或配置错误(错误的配置部分被确认是一个问题作为根据@Junaid Qadir 的评论).

In the case of the database driver, your DB connection might be wrong, or the sessions table might not exist or wrongly configured (the wrong configuration part was confirmed to be an issue as per the comment by @Junaid Qadir).

redis/memcached 配置错误或同时被系统中的某些其他代码操作.

redis/memcached configuration is wrong or is being manipulated by some other piece of code in the system at the same time.

执行 php artisan key:generate 并生成一个新的应用程序密钥可能是一个好主意,该密钥将依次刷新会话数据.

It might be a good idea to execute php artisan key:generate and generate a new app key which will, in turn, flush the session data.

清除浏览器缓存困难,我发现 Chrome 和 Firefox 是罪魁祸首,我记不清了.

Clear Browser Cache HARD, I found Chrome and Firefox being a culprit more than I can remember.

阅读更多关于为什么应用程序密钥重要

这篇关于在 Laravel 中发布请求 - 错误 - 419 抱歉,您的会话/419 您的页面已过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆