duanbi2760 2016-01-06 01: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 01: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 关于#c##的问题:treenode反序列化后获取不到上一节点和下一节点,Fullpath和Handle报错
  • ¥15 一部手机能否同时用不同的app进入不同的直播间?
  • ¥15 没输出运行不了什么问题
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing