如何从 Laravel 应用程序访问全局变量 $_SESSION 和 $_COOKIE?

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

How to access the globals $_SESSION and $_COOKIE from a laravel app?

phpsessioncookiesframeworkslaravel

提问by gvsrepins

I am working on a laravel project that needs to work besides a custom php project, because that I need to get access to the pure php globals $_SESSION and $_COOKIE in the laravel app.

我正在开发一个除自定义 php 项目外还需要工作的 laravel 项目,因为我需要在 laravel 应用程序中访问纯 php 全局变量 $_SESSION 和 $_COOKIE。

Until now I'm geting just null or empty. Somebody know how to do that?

到目前为止,我只是空的或空的。有人知道怎么做吗?

回答by Antonio Carlos Ribeiro

Laravel has its own implementations of $_SESSIONand $_COOKIE, so I'm afraid you cannot access them via those superglobals.

Laravel 有自己的$_SESSIONand实现$_COOKIE,所以恐怕你不能通过这些超全局变量访问它们。

To try to overcome that, what you can do, in your Laravel app, is to transfer data from one to the other by doing something like:

为了克服这个问题,您可以在 Laravel 应用程序中通过执行以下操作将数据从一个传输到另一个:

foreach(Session::all() $key => $value)
{
    $_SESSION[$key] = $value;
}

回答by gvsrepins

The answer for sessions is so simple that I don't believe at the begining that it will actually work.

会话的答案非常简单,以至于我一开始不相信它会真正起作用。

I was thinking that Laravel somehow override the default behavior of the normal PHP Sessions. Maybe declaring a custom session_handleror some other magic behind the scenes.

我在想 Laravel 以某种方式覆盖了普通 PHP 会话的默认行为。也许session_handler在幕后宣布习俗或其他一些魔法。

Thanks for all the answers I got here, I discover that Laravel doesn't actually use the $_SESSION at all, it implements a completely different driver.

感谢我在这里得到的所有答案,我发现 Laravel 实际上根本不使用 $_SESSION,它实现了一个完全不同的驱动程序。

So, to have acesses to the external session, I just use the native session_start()and session_name()in pure PHP and now I have acess to all data save in the global $_SESSIONon the same namespace used in the side project.

因此,要访问外部会话,我只使用本机session_start()session_name()纯 PHP,现在我可以访问所有数据,这些数据保存在$_SESSION辅助项目中使用的同一命名空间中的全局中。

For cookies, I can't figure out yet how to do it, from the Laravel app :/. But just with sessions I can go ahead and merge the apps.

对于 cookie,我无法从 Laravel 应用程序中弄清楚如何去做:/。但只要通过会话,我就可以继续合并应用程序。

回答by Wayne Whitty

I had a similar issue with CakePHP a couple of months ago. Basically, the session variables in the Cake application were completely independent of the session variables in the bespoke application that we had. The issue was that Cake uses a different session identifier than PHPSESSID. In the end, I was forced to bridge the gap by forcing Cake to use PHPSESSID. I'm not too familiar with Laravel, but from what I can see (on their main site), they seem to be using "laravel_session" instead of PHPSESSID. This is most likely your issue and there seems to be a topic about thison the offical Laravel forums.

几个月前,我在 CakePHP 上遇到了类似的问题。基本上,Cake 应用程序中的会话变量完全独立于我们拥有的定制应用程序中的会话变量。问题是 Cake 使用了与 PHPSESSID 不同的会话标识符。最后,我被迫通过强制 Cake 使用 PHPSESSID 来缩小差距。我对 Laravel 不太熟悉,但从我所看到的(在他们的主站点上),他们似乎使用“laravel_session”而不是 PHPSESSID。这很可能是您的问题,官方 Laravel 论坛上似乎有一个关于此主题

回答by Miroslav Trninic

Laravel has its own implementation of those globals, but you can access them over Symfony request class - which is parent of Laravel request. Symfony is working directly with globals.

Laravel 对这些全局变量有自己的实现,但您可以通过 Symfony 请求类访问它们——它是 Laravel 请求的父类。Symfony 直接使用全局变量。

Session:

会议:

Request::getSession();

Cookie:

曲奇饼:

$cookie = new ParameterBag($cookie_name);

回答by Alex Karagachev

I had similar problem with cookies, which weren't being set by laravel and Cookie::get() returned null. It's because laravel encrypts their cookies. You can just not include EncryptCookiesmiddleware to avoid this problem. Remove \App\Http\Middleware\EncryptCookies::classfrom $middlewareGroupsin app\Http\Kernel.php

我有类似的 cookie 问题,它们不是由 laravel 设置的,并且 Cookie::get() 返回 null。这是因为 Laravel 加密了他们的 cookie。您可以不包含EncryptCookies中间件来避免此问题。删除\App\Http\Middleware\EncryptCookies::class$middlewareGroupsapp\Http\Kernel.php