duanbi2760 2016-01-06 09:00
浏览 32
已采纳

来自阵列的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

  • 写回答

1条回答 默认 最新

  • doujiao6507 2016-01-06 09:29
    关注

    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']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题