2

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?

Josh
  • 8,082
  • 5
  • 43
  • 41
Amit
  • 49
  • 1
  • 3
  • 7
  • 2
    Please do not use `unserialize` on user-submitted data. This is easily exploitable with object injection using PHP's __wakeup and __destruct methods. You can use `json_encode/json_decode` instead of `serialize/unserialize`. https://www.owasp.org/index.php/PHP_Object_Injection – Quinn Comendant May 02 '14 at 23:52

2 Answers2

5

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,'/');
patrix
  • 66
  • 2
1

this link best simplify using Serialize and Unserialize data

https://stackoverflow.com/questions/9032007/arrays-in-cookies-php/9032082#9032082
Kamlesh Kumar
  • 1,632
  • 2
  • 21
  • 31