If you want the session or the cookies to be working over all of the domains, you'd have to specify this for the cookies or for the session.
This is how to do it in PHP. It works for named domain names, as well as for IP's and also for localhost or LAN server names, but not for composed TLD's (e.g. works for example.com, but not for example.co.uk):
if ( strpos($_SERVER["SERVER_NAME"], ".")!==false && preg_match("/([^0-9\.])/i", $_SERVER["SERVER_NAME"]) ) // check if there are any dots inside the server name and also letters (excludes IP's and LAN server names)
{
$p = preg_split("/\./i", $_SERVER["SERVER_NAME"], -1, PREG_SPLIT_NO_EMPTY); // split the domain name by dots (www, example, com)
while ( count($p)>2 ) array_shift($p); // push out all elements except the last two (example and com)
$COOKIE_PARAMS["dom"] = ".".join(".", $p); // this is what we need ".example.com"
}
else $COOKIE_PARAMS["dom"] = $_SERVER['SERVER_NAME']; // the full name of the server for IP's and local server names
$COOKIE_PARAMS["path"] = "/"; // the path under which the cookie is available - we'll use this also for the session
session_set_cookie_params (0, $COOKIE_PARAMS["path"], $COOKIE_PARAMS["dom"]);
// lifetime =0 (until browser is closed),
// path=$COOKIE_PARAMS["path"] (path under which the session is available),
// and the important part: domain=$COOKIE_PARAMS["dom"] (".example.com" so it works for all possible subdomains)
setcookie("mycookie", "myvalue", time()+30*60, $COOKIE_PARAMS["path"], $COOKIE_PARAMS["dom"], false); // set a cookie "mycookie" with the value "myvalue", lifetime 30 minutes, available under the path $COOKIE_PARAMS["path"]
This is it!
Questions? Comments?
Niciun comentariu:
Trimiteți un comentariu