PHP Sessions across sub domains
Configuring PHP sessions to span multiple subdomains requires a precise understanding of how the session.cookie_domain directive interacts with browser cookie scoping rules. The scenario often involves a primary application, like auth.example.com, handling authentication logic while several other applications, such as sub1.example.com and sub2.example.com, share those credentials.
The most common initial approach is setting session.cookie_domain = ".example.com" in your php.ini file or environment variables. This configuration tells the PHP session handler to set a cookie with that domain prefix. However, this setting alone does not guarantee that data stored in the session on one subdomain will be readable by another.
The core issue lies in how browsers handle cookies and how the PHP session mechanism interacts with them. When you execute session_start(), PHP checks for a session cookie. If a valid cookie exists that matches the current request's domain, path, and HTTP-only settings, PHP uses the associated session ID to retrieve the data from the server's storage (like Redis, memcached, or the filesystem).
In your specific test case, you observed that the session_id() printed on both sub1.example.com and auth.example.com was identical. This confirms that the cookie is being successfully passed from the subdomain to the auth domain. The browser recognized .example.com as a valid parent domain for the current request.
However, when you inspected $_SESSION, only the keys set on the current subdomain appeared. This happens because PHP treats the session storage as distinct per subdomain unless explicitly configured otherwise. Even though the session ID (the cookie) is shared, the actual session data files or database entries are often isolated by default in many configurations to prevent unauthorized access to sensitive data if a session hijacking attempt occurs.
To achieve true cross-subdomain session sharing, you must ensure that the session storage backend is not scoped by subdomain. By default, PHP's internal files handler creates a session file per domain or per specific path configuration depending on the server setup. More importantly, many hosting environments or application frameworks isolate sessions based on the HTTP_HOST header if the session.cookie_path and session.cookie_domain are not meticulously aligned with the storage driver's expectations.
The solution involves ensuring that when a session starts, PHP does not automatically create a new, isolated storage entry just because it sees a different host header, but rather looks up the existing ID in a shared store.
If you are using the default file-based storage:
- Ensure
session.cookie_domainis set to.example.com. - Ensure
session.cookie_pathis set to/. - Crucially, verify that your server configuration does not enforce session isolation per subdomain via
.htaccessor Nginx directives if you are using the built-in file handler.
If you are using a shared storage backend like Redis (common in Laravel or modern PHP stacks), the issue is often how the application initializes the session handler. You might need to explicitly call session_start() after setting the parameters, but ensure that the underlying driver treats the session ID as a universal key regardless of the host header.
A critical pitfall to watch for is the httponly and secure flags on your session cookie. If your auth domain serves content over HTTPS but your subdomains serve content over HTTP (or vice versa), the browser will block the cookie from being sent. This results in a new session ID being generated on every request, effectively breaking the shared state you are trying to build. Ensure that session.cookie_secure is set appropriately for your environment: 1 if all hosts use HTTPS, or 0 if they do not.
Additionally, check your application's logic. In your example code, you are setting $_SESSION['Regsitered'] = 1. If the session data is stored in a file named after the subdomain (e.g., /tmp/sess_sub1_...), then sub2 will never find that file because it expects a file named sess_sub2_... or simply a different directory structure. The session storage driver must be configured to use a single, unified namespace for all .example.com hosts.
If you are using a framework like Laravel, the config/session.php file often contains cookie settings and driver configuration. Ensure that the encrypt setting is consistent across your applications. If one app encrypts sessions with a key derived from its environment and another uses a different key or no encryption, the session ID might match, but the decryption will fail, or the data won't be recognized.
Finally, consider the trade-offs. Sharing session state across subdomains increases the blast radius if an attacker manages to steal a session ID. If one application is compromised, the attacker could potentially hijack sessions used by other applications in the same domain family. You must weigh the convenience of single sign-on against these security implications and ensure that your authentication tokens or session validation logic remains robust regardless of which subdomain initiated the request.