Forum Moderators: coopster
<?php
ini_set("session.cookie_domain", ".example.com");
// Set cookie and redirect when user change city
if( isset($_POST['city']) && $_POST['city'] != '' ){
$cookie_expire = time() + 50400;
setcookie('city', $_POST['city'], $cookie_expire, '/');
header("Location: http://".$_POST["city"].".example.com");
die();
}
// Redirect if user selected default city
if (isset($_COOKIE["city"])) {
$subdomain = array_shift(explode(".",$_SERVER['HTTP_HOST']));
if ($_COOKIE["city"] != $subdomain) {
header("Location: http://".$_COOKIE["city"].".example.com");
die();
}
}
The domain that the cookie is available to. Setting the domain to 'www.example.com' will make the cookie available in the www subdomain and higher subdomains. Cookies available to a lower domain, such as 'example.com' will be available to higher subdomains, such as 'www.example.com'. Older browsers still implementing the deprecated » RFC 2109 may require a leading . to match all subdomains.
[php.net...]
setcookie('city',$_POST['city'],$cookie_expire,'/','.example.com');
Main Domain:
<?php
function YOUR_MODULE_NAME_init() {
if(isset($_COOKIE['city'])) {
header('Location: ' . base64_decode($_COOKIE['city']));
}
}
Sub-domain
<?php
function YOUR_MODULE_NAME_init() {
if(!isset($_COOKIE['city'])) {
/*Your need to set cookie for your main domain, as well as for all your subdomains otherwise once a user visits another subdomain by typing the url directly in the address bar the cookie would be overwritten*/
setcookie('city', base64_encode($_SERVER['HTTP_HOST']), time()+50400, '/', 'example.com');
setcookie('city', base64_encode($_SERVER['HTTP_HOST']), time()+50400, '/', 'amsterdam.example.com');
setcookie('city', base64_encode($_SERVER['HTTP_HOST']), time()+50400, '/', 'newyork.example.com');
setcookie('city', base64_encode($_SERVER['HTTP_HOST']), time()+50400, '/', 'london.example.com');
}
else {
header('Location: ' . base64_decode($_COOKIE['city']));
}
}
?>
[edited by: JD_Toims at 4:09 am (utc) on Dec 11, 2013]