Preserving session variables across different domains

Preserving session variables across different domains

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

Stefan Bogdanescu

Founder & Senior Architect

Why Sessions Don't Cross Domain Boundaries by Default

It appears your intuition about how session variables work is partially correct, but there is a fundamental architectural barrier preventing $_SESSION data from simply "passing over" when you redirect from one domain to another. The short answer is no, the session variables will not carry over automatically if etm124annualgala.com and etm124biz.com are configured as distinct domains.

When you execute a Location header to redirect a user from the event site to the main payment site, the browser sends a new request. Crucially, this new request originates from a different host. HTTP sessions are scoped strictly to the domain (or subdomain) name and path defined in the cookie attributes. Unless both domains explicitly share the exact same session configuration, the etm124annualgala.com domain cannot send its session cookie to etm124biz.com, and consequently, the payment script on the main site will see an empty or uninitialized $_SESSION array.

The core issue lies in how cookies are stored and retrieved. By default, PHP sessions store a cookie named PHPSESSID. If your server is running on etm124annualgala.com, it sets this cookie with a domain attribute restricted to that specific host. When the user navigates to etm124biz.com, the browser does not automatically include that cookie because the domains do not match. Even if you manually configure the cookie on the event site to be accessible across subdomains, cross-domain session sharing requires strict adherence to security policies and often specific server configurations.

Implementing Cross-Domain Session Sharing

If you cannot refactor your payment code to accept $_GET parameters or a new POST payload, you must ensure both domains reference the same physical session storage mechanism and share the cookie domain correctly. This is typically achieved by configuring the PHP session handler and the cookie properties consistently on both servers.

On both etm124annualgala.com and etm124biz.com, your php.ini or .htaccess configuration must ensure that the session save path points to a shared storage location if you are using file-based sessions, or that the database credentials for MySQL sessions match exactly. However, the most critical setting is the cookie domain.

In your PHP code on both sites, you need to explicitly define the cookie domain before any output is sent:

session_set_cookie_params([
    'lifetime' => 0, 
    'path' => '/', 
    'domain' => '.etm124biz.com', // Includes main site and subdomains
    'secure' => true,             // Must be true if using HTTPS
    'httponly' => true,           // Recommended to prevent XSS access
    'samesite' => 'Lax'           // Or 'Strict' depending on your needs
]);
session_start();

Critical Constraint: For this to work, the cookie domain must include a leading dot (e.g., .etm124biz.com). This tells the browser to send the session cookie for etm124biz.com and any subdomain of that root. Since your event site (etm124annualgala.com) is not a subdomain of the main business domain, this specific configuration will fail unless you restructure the DNS so the event site becomes a subdomain (e.g., gala.etm124biz.com).

The Shared Storage Requirement

Sharing cookies is only half the battle; the session data must also be stored in a place both servers can read. If etm124annualgala.com saves sessions to /var/lib/php/sessions/event and etm124biz.com looks in /var/lib/php/sessions/main, the redirect will fail even if cookies are shared.

The robust solution is to configure both servers to use a centralized session storage backend. The most common approaches are:

  1. MySQL/PostgreSQL: Configure both PHP installations to save sessions to the same database using the same table structure and credentials.
  2. Redis: Use a Redis instance as the central store for session data, accessible by both servers via their respective connection strings.

If you stick with file-based sessions (session.save_handler = files), both directories must be on the same filesystem or mounted from the same volume, which is often impossible if the two sites are hosted on different servers entirely. In a distributed environment where the event site and main site are on different physical machines, a database or Redis backend is mandatory for session persistence across domains.

Architectural Alternatives

While forcing shared sessions works, it introduces security risks and tight coupling between your systems. If you have control over the payment flow, passing data via URL parameters (GET) or a POST payload is generally safer and more explicit. However, since you mentioned constraints on modifying the payment code to accept $_GET, the shared session approach described above is your primary option.

Ensure that the redirect URL includes valid data if possible, but given the strict requirement to keep the existing payment logic intact, aligning the session infrastructure as detailed is the only viable path forward without a significant refactor. Remember that secure must be true for these cookies to transmit over HTTPS, and any mismatch in SSL certificates or protocol versions between the two domains will break the connection immediately.

Our network