如何在Laravel中为多个域设置会话Cookie?

时间:2020-04-21 09:38:28

标签: php laravel session cookies

可以使用2个不同的域名www.4evergaming.comwww.best4games.com访问我的Laravel应用程序(这些不是我的实际域名,只是一个例子)

这是我的路线文件的外观

Route::pattern('domain', '(www.4evergaming.com|www.best4games.com)');
Route::domain('{domain}')->group(function ()
{
    // Routes here...
})

但是在我的config/session.php中,我只能拥有一个域...

'domain' => '.4evergaming.com',

我需要为两个域都设置会话cookie,但是我该怎么做呢?还是有某种方法可以跨域复制会话?

1 个答案:

答案 0 :(得分:0)

我通过在运行时更改配置来解决了这个问题。

Route::pattern('domain', '(www.4evergaming.com|www.best4games.com)');
Route::domain('{domain}')->group(function ()
{
    if (strpos($_SERVER['HTTP_HOST'], 'www.best4games.com') !== false) {
        config([ 'session.domain' => 'www.best4games.com' ]);
    }
})