2015-09-14 104 views
0

我想在一個cookie中存儲一個json字符串,但是,特殊字符,如; {"":""}得到編碼。我試過setrawcookie(),但它不存儲多個屬性值。如何在PHP中使用PHP存儲原始json字符串?

$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()方法實現此目的?

+1

那麼,什麼問題?被編碼的字符有什麼問題?當你需要閱讀它時,你不能解碼嗎? –

+1

您應該在設置時刪除您的stripslashes()調用。你需要逃避那些角色。只需在檢索cookie時將其刪除。 – Chris

+1

json上的stripslashes()可能會破壞json字符串。不要這樣做。 –

回答

1

當您使用setcookie()時,應該自動轉義特殊字符。您只需在檢索cookie後刪除斜線。

$array = array(....); 

setcookie($this->cookie_customs_name, json_encode($array), ...); 

當檢索該cookie:

$cookie = stripslashes($_COOKIE[$this->cookie_customs_name]); 
$cookie = json_decode($cookie); 

未經檢驗的,而應該是所有需要。

相關問題