note 104678 modified in function.session-set-cookie-params by danbrown

From: Date: Thu, 30 Jun 2011 13:43:42 +0000
Subject: note 104678 modified in function.session-set-cookie-params by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181103@lists.php.net to get a copy of this message
Setting the domain for cookies in session_set_cookie_params() only affects the domain used for the session cookie which is set by PHP. All other cookies set by calling the function setcookie() either: i) Use the domain set explicitly in the call to setcookie() or ii) Don't set the domain at all on the cookie and so the browser assumes it's for the current domain. So to make all your cookies be available across all sub-domains of your site you need to do this: <?php $currentCookieParams = session_get_cookie_params(); $rootDomain = '.example.com'; session_set_cookie_params( $currentCookieParams["lifetime"], $currentCookieParams["path"], $rootDomain, $currentCookieParams["secure"], $currentCookieParams["httponly"] ); session_name('mysessionname'); session_start(); setcookie($cookieName, $cookieValue, time() + 3600, '/', $rootDomain); ?> --was-- Setting the domain for cookies in session_set_cookie_params() only affects the domain used for the session cookie which is set by PHP. All other cookies set by calling the function setcookie() either: i) Use the domain set explicitly in the call to setcookie() or ii) Don't set the domain at all on the cookie and so the browser assumes it's for the current domain. So to make all your cookies be available across all sub-domains of your site you need to do this: $currentCookieParams = session_get_cookie_params(); $rootDomain = '.mywebsite.com'; session_set_cookie_params( $currentCookieParams["lifetime"], $currentCookieParams["path"], $rootDomain, $currentCookieParams["secure"], $currentCookieParams["httponly"] ); session_name('mysessionname'); session_start(); setcookie($cookieName, $cookieValue, time() + 3600, '/', $rootDomain); http://php.net/manual/en/function.session-set-cookie-params.php

« previous php.notes (#181103) next »