Multiple TLD cookie, with db lookup
Last updated on
3 September 2019
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
For use with Domain Access module only.
I got this idea from Multiple top level cookie domains
If you do not wish to modify 'settings.php' each time you add a top level domain (like me) with a new $cookie_domain then replace your where 'example.com' is your TLD:
$cookie_domain = 'example.com';
with this:
if (isset($_SERVER['HTTP_HOST'])) {
// remove any sub- subsub- etc domains
$_srv_host = explode(".", $_SERVER['HTTP_HOST']);
$srv_host = array_reverse($_srv_host);
$request_host = $srv_host[1] . "." . $srv_host[0]; // this gives example.com with ALL subdomains removed
// now we check against the database
// do not rewrite link info a second time
preg_match("/@(.*?)\//", $db_url, $link_host);
preg_match("/\/\/(.*?):/", $db_url, $link_user);
preg_match("/[^mysql(i)]:(.*?)@/", $db_url, $link_pass);
preg_match("/@(.*?)\/(.*?)$/", $db_url, $link_db);
$link = mysql_connect($link_host[1], $link_user[1], $link_pass[1]);
if ($link) {
if (mysql_select_db($link_db[2], $link)) {
/***********************************************/
/* no - we are not using {domain} as tablename */
/* Drupal is not yet loaded - remember? */
/* do not forget to add your table prefix */
/* to domain => prefix_domain */
/***********************************************/
$q = "SELECT subdomain, valid FROM domain WHERE subdomain LIKE '%" . $request_host . "' AND valid LIKE '1'";
$res = mysql_query($q);
if ($res) { // got answer
$rows = mysql_num_rows($res);
// yes, only one row - it's logical Mr. Spock
if ($rows == 1) {
$row = mysql_fetch_array($res);
$cookie_domain = "." . $row['subdomain'];
} else {
// we did get 1+ rows which is no-no, use the old settings with TLD
$cookie_domain = '.example.com';
}
}
}
} else {
// we did not get the link, use the old settings with TLD
$cookie_domain = '.example.com';
}
mysql_close($link); // behave
}
Note:
This uses database lookup - and nothing else - prior Drupal loads it's stuff.
Help improve this page
Page status: No known problems
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion