Little late to the show and UI have similar issue arrising in my development scheme of things. After banging my head here eand there obvious is clear.
Lets break it down there is a setter aka php script from a perticular domain and there is sender aka browser which sends cookies on every call made from the browser to domain.
We also know that once php script is done processing it losses connection to the browser and opens up thread for new call per say.
Broswer however uses cookies expiry date to determine what to keep in cache and what not to keep in cahche. Based on whats kept it kees on coupling the data to each call.
what we are intending to do is make script tell browser which domain to send cokkie to and which domain to not send cookie to.
Specification says only the domain which is the setter will recieve the cookie from sender. If it was not this way then we would be in lot of trouble. huge hacking gateway flooded here and there.
Based on above the php cookie function by virture only performs one operation yes we can regex bit here and there bit under the hood it is only performing single operations.
e.g.
setcookie('token', base64_encode(serialize($token)), time()+10800, '/', 'mydomain.com');
Above code is only perfoming one set of instruction as per rule of functional output. Function cannot output two outputs at the same time.
setcookie('token', base64_encode(serialize($token)), time()+10800, '/', '.mydomain.com');
The second code is also persforming single output even so '.mydomain.com' is is single output sintruction to browser. It is the browser which interputs what to do with it not php code.
now if we need to be very selctive we either have to perform two functional puts e.g.
setcookie('token', base64_encode(serialize($token)), time()+10800, '/', 'dev.mydomain.com');
setcookie('token', base64_encode(serialize($token)), time()+10800, '/', 'prod.mydomain.com');
above codes will run two sperate instructions an will limit coookies to selective domains only and so will browser too.
if we use reklative setcookie('token', base64_encode(serialize($token)), time()+10800, '/', '.mydomain.com');
then browser would use it as wild card and to be honest browser does not know whether it is meant to be selective or wikd card.
Thus only option is and it has its merits we need to rechoreogrpah our business logic and not just rely on wild carding or genric single output. Even if ther was a function it would run logic of sorts.
function newCookie ($name,$value = "",$expires = 0,$path = "/",$domain = "",$secure = false,$httponly = false){
if (is_array($domain) && sizeof($domain)>> 0){
foreach ($domain as $value) {
setcookie($name,$value,$expires,$path,$value,$secure,$httponly);
}
} else {
setcookie($name,$value,$expires,$path,$domain,$secure,$httponly);
}
};
newCookie('token', base64_encode(serialize($token_value)), time()+10800, '/', ['prod.mydomain.com', 'dev.mydomain.com']);
or simply function newCookie ($name,$value = "",$expires = 0,$path = "/",$domain = "",$secure = false,$httponly = false){
if (is_array($domain) && sizeof($domain)>> 0){
foreach ($domain as $value) {
setcookie($name,$value,$expires,$path,$value,$secure,$httponly);
}
} else {
setcookie($name,$value,$expires,$path,$domain,$secure,$httponly);
}
};
newCookie('token', base64_encode(serialize($token_value)), time()+10800, '/', 'dev.mydomain.com');
Bottom line is one has to update yoiur business logic as examplified above just do it once and it should work fine and browser would know percisely what to do.