9

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.

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

gvsrepins
  • 1,687
  • 2
  • 17
  • 20
  • 1
    In *any* PHP script you have access to superglobals like `$_SESSION` or `$_COOKIE` always (in HTTP SAPI context, CLI might vary). Getting NULL (or empty) just means that those are not used so far, which can certainly be the case, because some session management must not use `$_SESSION` at all. For [`$_COOKIE`](http://php.net/_cookie) it always contains all set cookies at the time of the HTTP request. Consult the technical documentation of Laravel not that it overwrites such superglobal array for some fun or so. – hakre Feb 26 '14 at 20:28

5 Answers5

5

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

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:

foreach(Session::all() $key => $value)
{
    $_SESSION[$key] = $value;
}
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • Unfortunately this doesn't help on this particular problem, because I need to implement the changes in the laravel app, because it is small and easily managed. But I know that this will help in others situations. thanks! – gvsrepins Feb 26 '14 at 20:50
  • How does it not? Anyway, I think this is the most relevant answer so far. More on [Laravel's Docs - Session (4.2)](http://laravel.com/docs/4.2/session). – srph Feb 24 '15 at 03:13
  • Because in his answer he shows how to make the laravel session visible to the other app, what is very useful, but what I needed at that time was the reverse. How to make the session of the other app available in the laravel app. Wich thanks to the answers I recived here I could do itn and was a really simple thing, check my own answer if you are curious about. :) – gvsrepins Mar 11 '15 at 14:04
2

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_handler or some other magic behind the scenes.

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.

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 $_SESSION on the same namespace used in the side project.

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.

gvsrepins
  • 1,687
  • 2
  • 17
  • 20
  • Great to hear, but I don't understand the reason you have to use ```$_SESSION``` when the ```Session``` abstraction exists. – srph Feb 24 '15 at 03:15
  • @srph this was for using $_SESSION info created from a non-laravel system, wich was the need for this specific projetc. – gvsrepins Aug 26 '15 at 20:04
1

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 this on the offical Laravel forums.

Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
0

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.

Session:

Request::getSession();

Cookie:

$cookie = new ParameterBag($cookie_name);
Miroslav Trninic
  • 3,327
  • 4
  • 28
  • 51
  • How this ParameterBag is supose to work? I'm sorry but I'm not seeing the relations with cookies. By the Symfony docs here: http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/ParameterBag.html, can you help me to figure it out? – gvsrepins Feb 26 '14 at 20:45
  • I try the session, but steal not working, the problem is, that the other app is saving the session on the default php folder. And the laravel app in you custom folder, may if I can override the laravel to use the default your answer will be right one. – gvsrepins Feb 26 '14 at 20:47
0

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 EncryptCookies middleware to avoid this problem. Remove \App\Http\Middleware\EncryptCookies::class from $middlewareGroups in app\Http\Kernel.php