【发布时间】:2015-09-14 14:45:11
【问题描述】:
我正在尝试将 json 字符串存储在 cookie 中,但是,特殊字符,例如; {"":""} 被编码。
我试过setrawcookie(),但它不会存储多个属性值。
$array = array('test' => 'value', 'anothertest' => 'not stored');
setrawcookie($this->cookie_customs_name, stripslashes(json_encode($array)),
strtotime($this->cookie_life_time), $this->cookie_path, $this->cookie_domain);
我在这里做错了什么?
另外,是否可以使用setcookie() 方法来实现这一点?
【问题讨论】:
-
那么,有什么问题呢?被编码的字符有什么问题?不能在需要阅读的时候直接解码吗?
-
您应该在设置时删除您的 stripslashes() 调用。您需要转义这些字符。只需在检索 cookie 时删除它们即可。
-
stripslashes() on json 可能会损坏 json 字符串。不要那样做。