Store array in cookie

笑着哭i 提交于 2019-12-24 04:21:47

问题


I am converting the array into cookie by php serialize function

$PromoteProductArray = array("PromoteuserId"=>$PromoteuserId,
"PromoteProductId"=>$PromoteProductId,
"PromoteBrandId"=>$PromoteBrandId);

$Promotedcart[] = $PromoteProductArray;

setcookie("Promotedcart", serialize($Promotedcart), time()+604800,'/');

And when the cookie is created then i am using the unserialize php function.

print_r(unserialize($_COOKIE['Promotedcart'])); 

it does not work.

When I print_R($_COOKIE) then it show me the value.


回答1:


Cookies separated by semicolon. Serialized strings with arrays contain them inside. Maybe this is a problem. You can use base64 to avoid all possible escape issues.




回答2:


You can use json_encode, json_decode functions to achieve this as an alternative.

$PromoteProductArray = array("PromoteuserId"=>$PromoteuserId,
"PromoteProductId"=>$PromoteProductId,
"PromoteBrandId"=>$PromoteBrandId);
$Promotedcart[] = $PromoteProductArray;
setcookie("Promotedcart", json_encode($Promotedcart), time()+604800,'/');
$result = json_decode($_COOKIE['Promotedcart'], true);
print_r($result);

Give it a try, this should work.



来源:https://stackoverflow.com/questions/7413161/store-array-in-cookie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!