Dmitriy Maletskiy
Dmitriy Maletskiy

Reputation: 11

PHP / Laravel: Share sessions across subdomain of each domain

I need to share session between: domain.com, sub1.domain.com sub2.domain.com

BUT also they must work with other domain. For example if i choose session.domain = ".domain.com" - sessions will not work with "another.com"

Thanks

Upvotes: 1

Views: 972

Answers (1)

Justin Origin Broadband
Justin Origin Broadband

Reputation: 1530

You cannot share a session across different domains as they are locked in by the session cookie domain setting.

You **can** retrieve associated data based on a cookie value that you can store in something such as Redis (server side). All it needs is some common identifier (such as a user ID) that is readable by another domain client side (by JavaScript).

The trouble is passing the values over from one site to another, or even another subdomain. You would need a central hub to connect to which stores the values and is the originator of the cookie domain. You can pass certain details over from one domain to another by referencing the cookie value stored on the second domain and pass data back (over ajax for example) based on the value you store.

Origin (Hub) Domain: cookie.domain.com
Subdomain 1:         subdomain1.domain.com
Subdomain 2:         subdomain2.domain.com
Different Domain:    www.differentdomain.com

Cookie Domain:     cookie.domain.com
JavaScript Domain: cookie.domain.com

This way, the JavaScript can read the cookie stored from cookie.domain.com, then interact with the current client side session or even the server/domain that the current page originates from.

If you have a look at how Facebook / Twitter inject their scripts and cookies, they come from the same domain / subdomain (that's how I figured this out for a previous project).

Facebook Cross Domain Login Toolkit

Upvotes: 0

Related Questions