Laravel Share Session Between Subdomains

To share sessions between subdomains in Laravel, you need to make a few configuration changes in your project.

First, update your `.env` file and set the `SESSION_DOMAIN` variable to the base domain of your project. For example, if your main domain is ``, set it as follows:

Next, you need to update the `config/session.php` file. Set the `domain` option to `’’` so that the session cookie is available across all subdomains:

    'domain' => '',

After making these changes, be sure to clear your Laravel application cache:

    php artisan cache:clear

Now you should be able to share sessions between subdomains. For example, if you have two subdomains `` and ``, the session data will be accessible on both subdomains.

In your code, you can access the shared session data using the standard Laravel session methods like `session(‘key’)` or `request()->session()->get(‘key’)`.

Here is an example of how you can set a session variable:

    session(['key' => 'value']);

And here is an example of how you can retrieve the session variable:

    $value = session('key');

Remember to replace `’key’` and `’value’` with your own key-value pairs.

That’s it! You should now have a better understanding of how to share sessions between subdomains in Laravel.

Leave a comment