Laravel在单独的域和子域中共享会话

时间:2018-11-13 13:04:32

标签: php laravel session

我有带有2个域example.com和example.net的Laravel应用。在同一个应用中,我需要两个不同的会话,例如example.com的会话和example.net和sub.example.net的不同会话。如果我将SESSION_DOMAIN设置为“ .example.net”会话,则example.com无法正常工作。对于所有域,我都有单独的控制器。有想法吗?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。需要在example.net所在的部分应用中使用中间件

namespace App\Http\Middleware;

use Closure;

class CookiesDomain
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        config(['session.domain' => '.example.net']);

        return $next($request);
    }
}

在cookie的默认配置中:

 /*
    |--------------------------------------------------------------------------
    | Session Cookie Domain
    |--------------------------------------------------------------------------
    |
    | Here you may change the domain of the cookie used to identify a session
    | in your application. This will determine which domains the cookie is
    | available to in your application. A sensible default has been set.
    |
    */

    'domain' => '.example.com',