diff --git a/esi.module b/esi.module
index bc88b94..fe1d859 100755
--- a/esi.module
+++ b/esi.module
@@ -235,8 +235,9 @@ function esi_user($op, &$edit, &$account, $category = NULL) {
 
   // Drupal session cookies use the name 'SESS' followed by an MD5 hash.
   // The role-cookie is the same, prefixes with the letter 'R'.
-  $role_cookie = array('name' => 'R' . session_name());
-  $user_cookie = array('name' => 'U' . session_name());
+  $cookie_params = session_get_cookie_params();
+  $role_cookie = $cookie_params + array('name' => 'R' . session_name());
+  $user_cookie = $cookie_params + array('name' => 'U' . session_name());
 
   if ($op == 'login') {
     require_once(drupal_get_path('module', 'esi') . '/esi.inc');
@@ -262,8 +263,10 @@ function esi_user($op, &$edit, &$account, $category = NULL) {
       'expire' => 1,
     );
   }
-  setcookie($role_cookie['name'], $role_cookie['value'], $role_cookie['expire'], ini_get('session.cookie_path'));
-  setcookie($user_cookie['name'], $user_cookie['value'], $user_cookie['expire'], ini_get('session.cookie_path'));
+  drupal_alter('esi_role_cookie', $role_cookie, $op, $account);
+  drupal_alter('esi_user_cookie', $user_cookie, $op, $account);
+  setcookie($role_cookie['name'], $role_cookie['value'], $role_cookie['expire'], $role_cookie['path'], $role_cookie['domain']);
+  setcookie($user_cookie['name'], $user_cookie['value'], $user_cookie['expire'], $user_cookie['path'], $user_cookie['domain']);
 }
 
 /**
