在Laravel中发布请求-错误-419对不起,您的会话已过期 [英] Post request in Laravel - Error - 419 Sorry, your session has expired

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

问题描述

我安装了Laravel 5.7

I installed Laravel 5.7

将表单添加到文件\resources\views\welcome.blade.php

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

已添加到文件\routes\web.php

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

发送POST请求后:

419对不起,您的会话已过期.请刷新,然后重试.

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

在版本5.6中,没有这样的问题.

In version 5.6 there was no such a problem.

推荐答案

在阅读以下内容之前,请确保您的表单中包含@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>

会话过期错误消息出现是因为您的csrf令牌验证失败的地方,这意味着App\Http\Middleware\VerifyCsrfToken::class中间件已打开. @csrf Blade指令的形式已经添加,也应该没问题.

The session expired error message comes up because somewhere your csrf token verification fails which means the App\Http\Middleware\VerifyCsrfToken::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.7,Laravel 5.8,Laravel 6中受支持的会话驱动程序 ( Doc Link)

  • file-会话存储在存储/框架/会话中.
  • 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).

执行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.

清除浏览器缓存 HARD ,我发现chrome和firefox是我不记得的罪魁祸首.

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

了解有关为何使用应用程序密钥的更多信息重要

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

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