来自阵列的Setcookie

来自阵列的Setcookie

问题描述:

I have an array cookies that i want to set on cilent browsers but setcookie doesn't seem to work.I have use cookie inspector on chrome to come to this conclusion

$cookies = json_encode($cookies);
var_dump($cookies);
setcookie("test", $cookies , 0 , '/' , 'anotherdomain.com');

var_dump result

string(246) "{"PHPSESSID":"6a6326e66daf90c61656c089165bf9af","__ntt":"deleted","first_visit":"1452068874","ref_code":"__default__","usertype":"Paid-User","marketing":"{\"user_cmp\":\"\",\"user_label\":\"\"}","localization":"{\"locale\":\"en\",\"db\":\"us\"}"}" cookie set

Edit : I needed to put the var_dump after the set cookie and for simplicity i removed the another domain in the set cookie

The code now works fine but its not quite what i intended.I want to set a cookie name PHPSESSID with value 6a6326e66daf90c61656c089165bf9af.Similar i want to create cookies and set their value for every element in array

我有一个数组cookie我想在静音浏览器上设置但是setcookie似乎不起作用。我有 使用chrome上的cookie检查器来得出这个结论 p>

  $ cookies = json_encode($ cookies); 
var_dump($ cookies); 
setcookie(“test”,$ cookies,  0,'/','anotherdomain.com'); 
  code>  pre> 
 
 

var_dump结果 p>

  string(246)  “{” PHPSESSID “:” 6a6326e66daf90c61656c089165bf9af “ ”__ NTT“: ”已删除“, ”first_visit“: ”1452068874“, ”ref_code“: ”__默认__“, ”用户类型“: ”付费用户“, ”市场营销“:”{  \ “user_cmp \”:\ “\”,\ “user_label \”:\ “\”} “ ”本土化“: ”{\“ 区域设置\ ”:\“ 恩\”,\ “DB \”:\”  us \“}”}“cookie set 
  code>  pre> 
 
 

编辑: 我需要将var_dump放在set cookie之后,为简单起见我删除了集合中的另一个域 cookie p>

代码现在工作正常,但它不是我想要的。我想设置一个cookie名称 PHPSESSID code>,其值为 6a6326e66daf90c61656c089165bf9af code> .Simi 我想创建cookie并为数组中的每个元素设置它们的值 p> div>

Create cookies and set their value for every element in array:

foreach($cookies as $name=>$val) {
    setcookie($name, $val , 0 , '/' , 'anotherdomain.com');
}

EDIT:

Just realised your $cookies is a multidimensional array. You need to json_encode every individual value:

foreach($cookies as $name=>$val) {
    setcookie($name, is_scalar($val)?$val:json_encode($val) , 0 , '/' , 'anotherdomain.com');
}

and json_decode them back when you use it:

$marketing = json_decode($_COOKIE['marketing']);