在php中更新cookie值

在php中更新cookie值,php,arrays,cookies,store,setcookie,Php,Arrays,Cookies,Store,Setcookie,我正在通过php序列化函数将数组转换为cookie $PromoteuserId='1'; $PromoteProductId='2'; $PromoteBrandId='3'; $PromoteProductArray = array("PromoteuserId"=>$PromoteuserId, "PromoteProductId"=>$PromoteProductId,

我正在通过php序列化函数将数组转换为cookie

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

$Promotedcart[] = $PromoteProductArray;

setcookie("Promotedcart", urlencode(serialize($Promotedcart)), time()+604800,'/');
当创建cookie时,我使用的是unserializephp函数

print_r(unserialize(urldecode($_COOKIE['Promotedcart'])));
我需要更新cookie值。例如,我需要在cookie中搜索PromoteProductId值是否为exit,如果它将更新cookie值CooResponse to PromoteProductId。
可以指导我如何更新该值吗?

您只需将未序列化的cookie存储到变量中,然后重置cookie即可

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

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

您可以简单地将未序列化的cookie存储到变量中,然后重置cookie

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

setcookie("Promotedcart", urlencode(serialize($array)), time()+604800,'/');
此链接最好简化 使用序列化和取消序列化数据

https://stackoverflow.com/questions/9032007/arrays-in-cookies-php/9032082#9032082
此链接最好简化 使用序列化和取消序列化数据

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

请不要对用户提交的数据使用
unserialize
。通过使用PHP的_wakeup和_destruct方法进行对象注入,可以很容易地利用此漏洞。您可以使用
json\u encode/json\u decode
而不是
serialize/unserialize
。请不要对用户提交的数据使用
unserialize
。通过使用PHP的_wakeup和_destruct方法进行对象注入,可以很容易地利用此漏洞。您可以使用
json\u encode/json\u decode
而不是
serialize/unserialize