Laravel, 2 projects in 2 domains of the same session

I am creating 2 projects in 2 different domains domain1.tld and domain2.tld.

domain1.tld is the main page of the event producer, and domain2.tld is one of its events. I want to share the same sessions (they actually use the same database and the same Apache server). I tried changing the session driver to a "database" and creating a session table, but nothing happens if I register domain1.tld, nothing happens in domain2.tld.

I really searched on the net but didn't find anything

+4
source share
2 answers

Step 1: Install Session Driver for Shared Session Data

, .

2:

Laravel. - , cookie . - : domain2.tld/? Session_token = abcds2342

- , , / ( ) . , :

session_id('abcds2342');
session_start();

, IP-,     - elses SessionID , ,   

2A. , StartSession. getSession , session_id cookie, , . :

<?php

namespace App\Http\Middleware;

use Illuminate\Session\Middleware\StartSession;
use Illuminate\Http\Request;

use App\SessionShare;

use Closure;

class StartSessionWithSharer extends StartSession
{

    /**
     * Get the session implementation from the manager.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Session\SessionInterface
     */
    public function getSession(Request $request)
    {
        $session = $this->manager->driver();

        /**
         * Check if we can find a valid session token from saved records
         */

            if($request->get('session_token') && !empty($request->get('session_token'))) {
                $sessionShare = SessionShare::valid()->whereToken($request->get('session_token'))->first();

                if($sessionShare)
                    $session_id = $sessionShare->session_id;
            }

        /**
         * Fallback to session in browser
         */

            if(!isset($session_id) || !$session_id)
                $session_id = $request->cookies->get($session->getName());

        $session->setId($session_id);

        return $session;
    }
}

2B. , SessionServiceProvider :

<?php namespace App\Providers;


class CustomSessionServiceProvider extends \Illuminate\Session\SessionServiceProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerSessionManager();

        $this->registerSessionDriver();

        $this->app->singleton('App\Http\Middleware\StartSessionWithSharer');
    }
}

SessionServiceProvider config/app.php .

2C: App\SessionShare . , IP-, , .

2D: , , , get session_token

, . , . , , ( 2C)

+2

...

, cookie, .

, cookie -, ( )

: - / - A - B - B cookie. , - A - B . - B , cookie A.

, .

+2

Source: https://habr.com/ru/post/1667193/


All Articles