update cookie value in php

烂漫一生 提交于 2019-12-18 09:35:59

问题


I am converting the array into cookie by php serialize function

$PromoteuserId='1';
$PromoteProductId='2';
$PromoteBrandId='3';
$PromoteProductArray = array("PromoteuserId"=>$PromoteuserId,
                            "PromoteProductId"=>$PromoteProductId,
                              "PromoteBrandId"=>$PromoteBrandId
                        );

$Promotedcart[] = $PromoteProductArray;

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

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

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

I need to update the cookie value. E.g. - I need to search for PromoteProductId value is exit in the cookie if then it will update the cookie value coorespond to PromoteProductId . could guide me how i can update the value?


回答1:


You can simply store the unserialized cookie into a variable then reset the cookie?

$array = unserialize(urldecode($_COOKIE['Promotedcart']));  
$array[0]["PromoteuserId"] = "New";

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



回答2:


this link best simplify using Serialize and Unserialize data

https://stackoverflow.com/questions/9032007/arrays-in-cookies-php/9032082#9032082


来源:https://stackoverflow.com/questions/7425774/update-cookie-value-in-php

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