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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c