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

niborocin's avatar

Subdomain Session Cookie - Multi Tenancy with Central Login

I use stancl/tenancy. My application has a central login located at localhost:8000. When I login I get redirected to the dashboard, all fine. But when I try to enter a subdomain of a tenant like example.localhost:8000 I get redirected back to the central domain.

This is because my session cookie's domain is set to localhost in the browser. When I manually change my cookie's domain to example.localhost and try to access example.localhost:8000, I can access the site and won't get redirected.

		#config/session.php

		SESSION_CONNECTION=mysql #session cookies are stored in the central db
		SESSION_DRIVER=database
		SESSION_LIFETIME=525600
		SESSION_ENCRYPT=false
		SESSION_PATH=/
		SESSION_DOMAIN=null #tried setting this to .localhost but the cookie's domain stays as localhost

Can't I assign my session cookie's domain something like a wildcard? *.localhost

What's the right way to solve this?

0 likes
1 reply
niborocin's avatar

I just learned that localhost can't be used for cookies on subdomains. So what I did was edit the hosts file on

C:\Windows\System32\drivers\etc

I mapped the 127.0.0.1 to my desired host names. Just add it to the end of the file:

127.0.0.1 example.test
127.0.0.1 tenant-1.example.test

Finally I confiured my .env and config/tenancy.php like this:

# .env
APP_URL=http://example.test:8000
SESSION_DOMAIN=.example.test

# config/tenancy.php
'central_domains' => [
	'127.0.0.1',
	'localhost',
	'example.test'
]

Hope this helps anyone else having this issue :) Even tho that's just one way of solving it

Please or to participate in this conversation.