Laravel会话始终会更改Laravel 5.4中的每个刷新/请求 [英] Laravel Session always changes every refresh / request in Laravel 5.4

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

问题描述

所以标题基本上说明了一切,我使用的是Laravel 5.4,PHP 7.1,并且在我的本地计算机上的会话工作正常.本质上,每次我尝试获取新的CSRF令牌时,尝试登录或重新加载页面时,一切都会中断.我将数据库用作会话驱动程序,它在我提出的每个请求中都向数据库创建了一个新条目.第一次升级时,这是在本地计算机上发生的,但要解决此问题,解决方案是更新我的cookie_domain env变量,并使它正常工作.但是,在我的新服务器上,我尝试了我能想到的每个域,但仍然无法正常工作.

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.

我认为这是某个地方的配置问题,但我不知道,任何帮助将不胜感激.如果有帮助,请在我的响应下查找我的初始页面标题,同时在"Set-Cookie"命令下同时显示sid和laravel会话,但实际上均未保存到本地应用程序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台服务器,所有3台服务器都在一个克隆映像上运行,这意味着它们是相同的,但是其中一个是子域development.mysite.com,另外两个是负载均衡的,主网站mysite.com.因此,我只能提出的唯一解释是,由于我的子域,laravel无法理解我的设置.我已经尝试了.env文件中的COOKIE_DOMAIN的所有可能组合(development.mysite.com,.mysite.com,mysite.com,.development.mysite.com等),并且我为主机添加了数十种选择有用.关于这可能是什么想法?

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会话始终会更改Laravel 5.4中的每个刷新/请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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