Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nzmattman's avatar

Session domains across multiple domains

Hi team.

so my current situation.

We have an app that has multiple domains plus.example.co.nz admin.plus.example.co.nz app.plus.example.co.nz api.plus.example.co.nz

which is all working well with the session domain of .plus.example.co.nz

I have to setup a stage site as well ideally it would be

stage.plus.example.co.nz stage.admin.plus.example.co.nz stage.app.plus.example.co.nz stage.api.plus.example.co.nz

if I put the session domain to .stage.plus.example.co.nz is that going to cause conflicts with the .plus.example.co.nz session domain?

0 likes
1 reply
LaryAI's avatar
Level 58

To address your question about session domains across multiple subdomains, let's break it down:

  1. Current Setup:

    • You have multiple subdomains under plus.example.co.nz and are using .plus.example.co.nz as the session domain. This setup allows sharing sessions across all subdomains under plus.example.co.nz.
  2. Stage Setup:

    • You want to set up a staging environment with subdomains under stage.plus.example.co.nz.

Solution

If you set the session domain to .stage.plus.example.co.nz for your staging environment, it will not conflict with the session domain .plus.example.co.nz used in your production environment. This is because session cookies set for .stage.plus.example.co.nz will only be accessible to subdomains under stage.plus.example.co.nz and not to subdomains under plus.example.co.nz.

Example Configuration

Here’s how you can configure your session domains in a Laravel application:

Production Environment (.plus.example.co.nz)

In your config/session.php file, set the session domain to .plus.example.co.nz:

'domain' => env('SESSION_DOMAIN', '.plus.example.co.nz'),

Staging Environment (.stage.plus.example.co.nz)

For your staging environment, you can set the session domain to .stage.plus.example.co.nz in your .env file:

SESSION_DOMAIN=.stage.plus.example.co.nz

This ensures that sessions are isolated between your production and staging environments.

Summary

  • Production: Uses .plus.example.co.nz as the session domain.
  • Staging: Uses .stage.plus.example.co.nz as the session domain.

By setting the session domains appropriately, you ensure that sessions are scoped correctly to their respective environments, avoiding any conflicts.

If you have any further questions or need additional clarification, feel free to ask!

Please or to participate in this conversation.