Multiple top level cookie domains

Last updated on
21 March 2018

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

If you are only using sub domains and don't intend to add any top level domains, then just setting the cookie domain in your settings.php file works fine (example: $cookie_domain = '.topdomain.com';).

However, if you intend to add another top level domain as an affiliate you won't be able to log on to the new top level domain by using the above. The above will only add a cookie to the domain you just named and since Drupal uses cookies to store session data, you won't be able to log on to the other top level domain. The solution is to set the cookie with a conditional statement. E.G.:

if (isset($_SERVER['HTTP_HOST']) && substr_count($_SERVER['HTTP_HOST'], 'parentdomain.com') > 0) {
$cookie_domain = '.parentdomain.com';
}

Now you can also login to the other top level domain as it has not satisfied the condition of the conditional statement above. If you also want to add wildcard sub domains to other top level domains, just continue the above condition with an "else" for each top level domain you want to use.

Note: This will only work if your domains are pointed to the default sites folder.

Thanks to Agent Rickard for the code above.

Help improve this page

Page status: No known problems

You can: