开发者

Store array in cookie [duplicate]

开发者 https://www.devze.com 2023-04-04 14:01 出处:网络
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
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

精彩评论

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

关注公众号