开发者

Store array in cookie [duplicate]

This question already has answers here: Storing PHP arrays in cookies (9 answers) Closed 1 year ago.

I am converting the array into cookie by php serialize function

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

$Prom开发者_开发问答otedcart[] = $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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜