【问题标题】:Store array in cookie [duplicate]将数组存储在cookie中[重复]
【发布时间】:2011-11-16 19:25:29
【问题描述】:

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

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

$Promotedcart[] = $PromoteProductArray;

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

当创建 cookie 时,我正在使用反序列化 php 函数。

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

它不起作用。

当我print_R($_COOKIE) 时,它会告诉我价值。

【问题讨论】:

  • 测试数据很有帮助,你能不能贴出样本
  • See this StackOverflow question 以获得更好的答案。
  • 请不要在用户提交的数据上使用unserialize。这很容易通过使用 PHP 的 __wakeup 和 __destruct 方法的对象注入来利用。您可以使用json_encode/json_decode 代替serialize/unserializeowasp.org/index.php/PHP_Object_Injection

标签: php arrays serialization cookies


【解决方案1】:

您可以使用json_encodejson_decode 函数作为替代方法来实现此目的。

$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);

试一试,应该可以的。

【讨论】:

    【解决方案2】:

    以分号分隔的 Cookie。带有数组的序列化字符串包含在其中。也许这是一个问题。您可以使用 base64 来避免所有可能的转义问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 2012-02-20
      相关资源
      最近更新 更多