Allow PHP sessions to carry over to subdomains

Allow PHP sessions to carry over to subdomains

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Allow PHP sessions to carry over to subdomains

When a user navigates from mydomain.example to user.mydomain.example, losing their session state is a classic misconfiguration issue. If you rely on PHP sessions rather than server-side tokens or database lookups for authentication, the default behavior of session_start() often treats subdomains as entirely different origins. This happens because the session ID cookie, which carries the session token, defaults to the exact hostname provided in the script's execution context.

By default, the session.cookie_domain directive is set to the current host without any wildcarding. When a user hits your main domain, the browser stores the session cookie with a path and domain scoped specifically to that host. Once the browser redirects or navigates to a subdomain like user.mydomain.example, it does not send the cookie because the domain in the cookie attribute (mydomain.example) does not match the current request host (user.mydomain.example). The result is a fresh session, effectively logging the user out immediately upon entry.

To resolve this, you must explicitly configure the PHP runtime to accept cookies from any subdomain under your primary domain. This is controlled by the session.cookie_domain configuration setting. You have two primary ways to apply this fix: via .htaccess for Apache environments or by modifying php.ini directly.

If you are using Apache with mod_rewrite enabled, adding the following line to your virtual host configuration or a global .htaccess file is the most straightforward approach:

<IfModule mod_php7.c>
    php_value session.cookie_domain .example.com
</IfModule>

Notice the leading dot before example.com. This syntax tells PHP and the underlying web server to match the domain against any subdomain hierarchy. Without that dot, the configuration would only apply if the request matched the exact string provided. With the dot included, session.cookie_domain becomes .example.com, which matches mydomain.example, user.mydomain.example, and even nested subdomains like app.dev.mydomain.example.

Alternatively, you can set this in your application's configuration files or directly in php.ini:

session.cookie_domain = .example.com

This approach is useful if you are running PHP as an FPM service or via a different web server architecture like Nginx with PHP-FPM. Ensure that the path is also correct; by default, session.cookie_path is /, which covers the root of your site. If your application runs from a subdirectory, such as /app, you must explicitly set session.cookie_path = /app to prevent the cookie from being scoped too narrowly, effectively blocking it from being sent in requests that don't match that specific path.

There is a critical security trade-off here: session fixation and cross-site request forgery (CSRF). By allowing cookies across all subdomains, you are trusting that every subdomain under .example.com will not be compromised or manipulated to steal the session ID. If a malicious actor gains control of evil.mydomain.example, they could potentially intercept the session cookie if your application is vulnerable to cross-subdomain session attacks. Mitigation strategies include setting the Secure flag on the session cookie (requiring HTTPS) and ensuring the HttpOnly flag is enabled so that JavaScript cannot read the cookie value via document.cookie.

In modern frameworks like Laravel, this configuration is often handled automatically if you use the standard session driver, but custom setups or older PHP versions might require manual intervention. If you are using a shared hosting environment where you cannot modify php.ini directly, adding the directive to your .htaccess file as shown above is usually sufficient and effective.

Finally, verify that your application logic does not explicitly regenerate the session ID upon every request or subdomain transition without maintaining state. While regenerating the session ID helps prevent fixation attacks, it must be done carefully to avoid unintentionally logging out users who have just moved between trusted subdomains.

Our network