【问题标题】:How to use same session on one laravel application using 2 different domains如何使用 2 个不同的域在一个 laravel 应用程序上使用相同的会话
【发布时间】:2018-03-20 13:39:39
【问题描述】:

我有一个 Laravel 5.3 应用程序。使用相同的数据库/服务器/应用程序。 但是我为同一个应用使用了 2 个不同的域(不是子域)。

domain1.com domain2.com

我希望当用户登录 domain1.com 时,该会话也可以在 domain2.com 上运行。但不会工作,不会有一个简单的解决方案,因为我使用相同的 laravel 应用程序?

我用路由来定义什么域用什么路由

这样的例子

Route::group(array('domain' => 'domain1'), function() {
//routes goes here
}

Route::group(array('domain' => 'domain2'), function() {
//routes goes here
}

【问题讨论】:

    标签: php laravel session


    【解决方案1】:

    如果您在会话中添加了域名,那么您可以在session.php 中的config 文件夹中设置null 并清除现有会话(laravel 5.5)

     /*
        |--------------------------------------------------------------------------
        | 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' => env('SESSION_DOMAIN', null),
    

    对于旧版本

    /*
        |--------------------------------------------------------------------------
        | 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' => null,
    

    更新

    如果仍然无法正常工作,则将会话保存在数据库中 运行命令 tp 生成表

    php artisan session:table
    

    //下面的命令将更新更改

    composer dump-autoload
    

    php 工匠迁移

    .env 更改为 SESSION_DRIVER=database

    同时修改config/session.php

    'driver' => env('SESSION_DRIVER', 'database') 
    'domain' => '.com'
    

    【讨论】:

    • 不,我没有在那里添加任何东西。它设置为 null :/
    • 你可以尝试将 'domain' => '.com' 更改为 .com,因为它的域不同
    • 我添加了 SESSION_DRIVER=database ,但是当我登录 domain1.com 时仍然不会在 domain2.com 上自动登录...如果我还添加了 'domain' => '.co.uk'它根本不会让我登录,只有当我将它设置为 null 作品时.. 但仍然不能将会话保持在 2 个域中.. hm
    • @mendi19.okay。我没有机会测试你的情况。看看我是否得到改变我会检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多