diff --git a/bakery.module b/bakery.module
index 72a41bc..2c10dc2 100644
--- a/bakery.module
+++ b/bakery.module
@@ -790,6 +790,27 @@ function bakery_user_page() {
 }
 
 /**
+ * Generate the name for a bakery cookie, this prevents domain
+ * mismatch on subdomains that are not part of a parent domains bakery config.
+ *
+ * @param string $type
+ *   CHOCOLATECHIP or OATMEAL, default CHOCOLATECHIP
+ * @return string
+ *   The namespaced cookie name for this environment.
+ */
+function _bakery_cookie_name($type = 'CHOCOLATECHIP') {
+  // Use different names for HTTPS and HTTP to prevent a cookie collision.
+  if (ini_get('session.cookie_secure')) {
+    $type .= 'SSL';
+  }
+  // Namespace the cookie by the cookie domain.  Use md5 to avoid using any
+  // reserved cookie terms (all of which contain non-hex characters):
+  //   "expires", "domain", "path", and "secure".
+  $name = $type . md5(variable_get('bakery_domain', ''));
+  return $name;
+}
+
+/**
  * Function to validate cookies
  *
  * @param $type (string) CHOCOLATECHIP or OATMEAL, default CHOCOLATECHIP
@@ -799,10 +820,7 @@ function bakery_user_page() {
 function _bakery_validate_cookie($type = 'CHOCOLATECHIP') {
   $key = variable_get('bakery_key', '');
 
-  $cookie_secure = ini_get('session.cookie_secure');
-  if ($cookie_secure) {
-    $type .= 'SSL';
-  }
+  $type = _bakery_cookie_name($type);
 
   if (!isset($_COOKIE[$type]) || !$key || !variable_get('bakery_domain', '')) {
     return;
@@ -1029,10 +1047,7 @@ function _bakery_bake_chocolatechip_cookie($name, $mail, $init) {
     $cookie['signature'] = hash_hmac('sha256', $cookie['name'] . '/' . $cookie['mail'] . '/' . $cookie['timestamp'], $key);
     // Use different names for HTTP and HTTPS to prevent a cookie collision.
     $cookie_secure = ini_get('session.cookie_secure');
-    $type = 'CHOCOLATECHIP';
-    if ($cookie_secure) {
-      $type .= 'SSL';
-    }
+    $type = _bakery_cookie_name('CHOCOLATECHIP');
     setcookie($type, bakery_mix(serialize($cookie), 1), $_SERVER['REQUEST_TIME'] + variable_get('bakery_freshness', '3600'), '/', variable_get('bakery_domain', ''), (empty($cookie_secure) ? FALSE : TRUE));
   }
 }
@@ -1040,10 +1055,7 @@ function _bakery_bake_chocolatechip_cookie($name, $mail, $init) {
 function bakery_taste_oatmeal_cookie() {
   $key = variable_get('bakery_key', '');
 
-  $type = 'OATMEAL';
-  if (ini_get('session.cookie_secure')) {
-    $type .= 'SSL';
-  }
+  $type = _bakery_cookie_name('OATMEAL');
 
   if (!isset($_COOKIE[$type]) || !$key || !variable_get('bakery_domain', '')) {
     return;
@@ -1067,6 +1079,7 @@ function bakery_taste_oatmeal_cookie() {
 function bakery_bake_oatmeal_cookie($name, $data) {
 
   $key = variable_get('bakery_key', '');
+  $cookie_secure = ini_get('session.cookie_secure');
   if (!empty($key)) {
     global $base_url;
     $cookie = array(
@@ -1083,12 +1096,7 @@ function bakery_bake_oatmeal_cookie($name, $data) {
       $cookie['slave'] = $base_url . '/'; // Match the way slaves are set in Bakery settings, with ending slash.
     }
     $cookie['signature'] = hash_hmac('sha256', $name . '/' . $cookie['timestamp'], $key);
-    // Use different names for HTTP and HTTPS to prevent a cookie collision.
-    $cookie_secure = ini_get('session.cookie_secure');
-    $type = 'OATMEAL';
-    if ($cookie_secure) {
-      $type .= 'SSL';
-    }
+    $type = _bakery_cookie_name('OATMEAL');
     setcookie($type, bakery_mix(serialize($cookie), 1), $_SERVER['REQUEST_TIME'] + variable_get('bakery_freshness', '3600'), '/', variable_get('bakery_domain', ''), (empty($cookie_secure) ? FALSE : TRUE));
   }
 }
@@ -1359,9 +1367,7 @@ function bakery_eat_gingerbread_cookie() {
  */
 function _bakery_eat_cookie($type = 'CHOCOLATECHIP') {
   $cookie_secure = ini_get('session.cookie_secure');
-  if ($cookie_secure) {
-    $type .= 'SSL';
-  }
+  $type = _bakery_cookie_name($type);
   setcookie($type, '', $_SERVER['REQUEST_TIME'] - 3600, '/', '', (empty($cookie_secure) ? FALSE : TRUE));
   setcookie($type, '', $_SERVER['REQUEST_TIME'] - 3600, '/', variable_get('bakery_domain', ''), (empty($cookie_secure) ? FALSE : TRUE));
 }
