php对象序列化和cookie的问题,反序列化false
$searchKeywords = array("羊奶","肥皂"); $searchKeywords = serialize($searchKeywords);//序列化 echo $searchKeywords; setcookie("searchKeywords",$searchKeywords, time()+3600*24); var_dump($_COOKIE['searchKeywords']);//查看存入的值 var_dump(unserialize($_COOKIE['searchKeywords']));//反序列化
看结果:
由图得知:
序列化后的字符串如果存cookie, 会自动处理,双引号加了斜线, 导致从cookie取出来的值在反序列化就会出错false.
我暂时解决方法是:
$KeywordsStr = $_COOKIE['searchKeywords']; $KeywordsStr= str_replace('"', '"', $KeywordsStr);