Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andrewclark's avatar

Sharing Cookies between Applications

I'm trying to share a Laravel 5.1 cookie with another application on a sub-domain (not Laravel) which will allow me to determine if the user is logged into the parent app - and automatically log them into this area.

The code below is where I am having issues - it worked fine with Laravel 4.2 but I recently upgraded to 5.1 and its no longer working. Having spent days going through the internals of Illuminate's Cookie & Session packages I'm no further forward.

public static function getCurrentLaravelSession() 
    {
        $app = sspmod_laravelauth_LaravelContainer::getInstance();

        if (!array_key_exists('laravel_session', $_COOKIE)) {
            return false;
        }
        
        $id = $app['encrypter']->decrypt($_COOKIE['laravel_session']);
        $request = Request::capture();

        $session = $app['session']->driver();
        $session->setId($id);
        $session->setRequestOnHandler($request);
        $session->start();
        
        dd($session->all());
        
        dd($app['auth']->check());

        return false;
    }

When the program gets to:

 dd($session->all());

the data from the session cookie does not match that which is saved by the parent application. Has anyone ever done something similar or know of a better way to go about determining if a user is logged in?

Any and all help is much appreciated!

0 likes
2 replies
andrewclark's avatar
andrewclark
OP
Best Answer
Level 1

New Method:

public static function getCurrentLaravelSession() 
    {
        $app = sspmod_laravelauth_LaravelContainer::getInstance();
        
        $app->make('Illuminate\Contracts\Http\Kernel')->handle(Illuminate\Http\Request::capture());

        if (!array_key_exists('laravel_session', $_COOKIE)) {
            return false;
        }
        
        $id = $app['encrypter']->decrypt($_COOKIE['laravel_session']);

        $session = $app['session']->driver();
        $session->setId($id);
        $session->setRequestOnHandler(Request::capture());
        $session->start();
        
        if ($app['auth']->check()) {
            return $app['auth']->user();
        }

        return false;
    }

Please or to participate in this conversation.