Laravel Session 总是在 Laravel 5.4 中更改每次刷新/请求 [英] Laravel Session always changes every refresh / request in Laravel 5.4

查看:17
本文介绍了Laravel Session 总是在 Laravel 5.4 中更改每次刷新/请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以标题基本上说明了一切,我使用的是 Laravel 5.4、PHP 7.1,并且在我的本地机器上会话工作得很好.基本上是在每次我获得一个新的 CSRF 令牌时尝试登录或重新加载页面时,这会破坏一切.我使用数据库作为我的会话驱动程序,它会在我发出的每个请求中创建一个新条目到数据库中.当我第一次升级时,这发生在我的本地机器上,但要修复它,解决方案是更新我的 cookie_domain 环境变量,我让它工作了.但是,在我的新服务器上,我尝试了我能想到的所有域,但仍然无法正常工作.

So the title basically says it all, I am using Laravel 5.4, PHP 7.1 and on my local machine sessions are working just fine. Essentially when trying to login or reloading the page every time I get a new CSRF token which breaks everything. I am using database as my session driver and it creates a new entry into the DB every single request I make. This happened on my local machine when I first upgraded but to fix it the solution was to update my cookie_domain env variable and I got it working. However on my new server I have tried every domain I could think of and it still does not work.

这是我所知道的,

  • 会话 cookie 未保存在 Chrome -> 应用程序 -> Cookies 下,这确实显示在我的本地设置中.

  • Session cookie is not being saved under Chrome -> Application -> Cookies, this is indeed showing up on my local setup.

CSRF-TOKEN 每次页面重新加载都会获得一个新值,但 XSRF-TOKEN cookie 存在并在每次重新加载时保持其值.

CSRF-TOKEN gets a new value every page reload but the XSRF-TOKEN cookie is present and maintains its value on every reload.

本地服务器和我的新服务器都运行完全相同的 git 分支,并且都运行 apache 2.4、Laravel 5.4 和 PHP 7.1,所以这让我认为这是某种配置问题.本地和服务器都运行centos 7

Both local and my new server are running the exact same git branch, and are both running apache 2.4, Laravel 5.4 and PHP 7.1 so this makes me assume it is a config issue of some kind. Both local and server are running centos 7

每个请求都会在数据库中创建一个新会话,这发生在 get、post 和 ajax 请求中.

Every request creates a new session in the DB, this happens for get, post and ajax requests.

我认为这是某个地方的配置问题,但我不知道,任何帮助将不胜感激.如果有帮助,请在我的初始页面加载的响应标题下查看 sid 和 laravel 会话都存在于Set-Cookie"命令下,但实际上都没有保存到本地应用程序 cookie 存储中.

I assume this is a config issue from somewhere but I have no idea, any help would be greatly appreciated. If it helps, looking under my response Headers for my initial page load both sid and the laravel session are present under the 'Set-Cookie" command, but neither actually save to the local application cookie storage.

编辑/更新所以最初我回答了我自己的问题,说我认为是修复,但是这个修复不再有效,我到处都遇到这个问题.所以更新我的问题和以前一样,尝试做任何事情都会创建一个新的会话,由于 ajax 请求和其他此类活动,重新加载和导航几个页面会添加数十个会话.

Edit / Update So originally I answered my own question by saying what I thought was the fix, however this fix is no longer working and I am getting this issue all over the place. So to update my issue is the same as before, trying to do anything creates a new session, reloading and navigating a few pages adds dozens of sessions due to ajax requests and other such activity.

我现在的设置是这样的,我有 3 台服务器,所有三台服务器都在运行克隆图像,这意味着它们是相同的,但是其中一个是子域,development.mysite.com,另外 2 台为主站点 mysite.com.所以我能想出的唯一可能的解释是,由于我的子域,laravel 无法理解我的设置.我已经尝试了 .env 文件(development.mysite.com、.mysite.com、mysite.com、.development.mysite.com 等)中 COOKIE_DOMAIN 的所有可能组合,并且我向我的主机添加了许多选项,全部都没有有用.关于这可能是什么的任何想法?

My setup now is this, I have 3 servers all three are running off of a cloned image which means they are identical, however one of them is a subdomain, development.mysite.com and the other 2 are load balanced for the main site mysite.com. So the ONLY possible explanation I can come up with is that somehow laravel cannot understand my setup due to my subdomain. I have tried every possible combination of COOKIE_DOMAIN in the .env file (development.mysite.com, .mysite.com, mysite.com, .development.mysite.com etc) and I have added dozens of options to my host all to no avail. Any ideas as to what this could be?

**编辑/更新#2 **除此之外,我发现它可能与我使用的域是一个子域有关,其中主域也在运行 laravel,如果是这样,我将不得不弄清楚如何以及为什么.

**Edit / Update #2 ** In addition to this I have found that it might be related to my domain in use being a subdomain, in which the main domain is also running laravel, if so I will have to figure out how and why.

推荐答案

我发现这个问题的解决方案分为两部分,不知道为什么会有所不同,因为它是相同的操作系统和设置.

I found the solution to this was 2 parts, not sure why it varied since it was the same OS and setup.

步骤 1 确保 COOKIE_DOMAIN 设置正确且没有端口号(在 .env 中或直接在/config/session.php 中,无论您使用哪个)

Step 1 make sure that COOKIE_DOMAIN is set properly and with no port numbers (Either in .env or directly in /config/session.php, whichever you use)

第 2 步确保/config/sessions.php 中的 cookie 名称 ('cookie' => 'whatever') 中没有下划线.Laravel 显然对此有问题.

Step 2 make sure that the cookie name ( 'cookie' => 'whatever') inside of /config/sessions.php does NOT have an underscore in it. Laravel apparently has had issues with this.

这篇关于Laravel Session 总是在 Laravel 5.4 中更改每次刷新/请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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