轉自:http://qing.weibo.com/tag/unserialize
把復雜的數據類型壓縮到一個字符串中
serialize() 把變量和它們的值編碼成文本形式unserialize() 恢復原先變量eg:$stooges = array('Moe','Larry','Curly');$new = serialize($stooges);PRint_r($new);echo "<br />";print_r(unserialize($new));結果:a:3:{i:0;s:3:"Moe";i:1;s:5:"Larry";i:2;s:5:"Curly";}Array ( [0] => Moe [1] => Larry [2] => Curly )當把這些序列化的數據放在URL中在頁面之間會傳遞時,需要對這些數據調用urlencode(),以確保在其中的URL元字符進行處理:$shopping = array('Poppy seed bagel' => 2,'Plain Bagel' =>1,'Lox' =>4);echo '<a href="next.php?cart='.urlencode(serialize($shopping)).'">next</a>';margic_quotes_gpc和magic_quotes_runtime配置項的設置會影響傳遞到unserialize()中的數據。如果magic_quotes_gpc項是啟用的,那么在URL、POST變量以及cookies中傳遞的數據在反序列化之前必須用stripslashes()進行處理:$new_cart = unserialize(stripslashes($cart)); //如果magic_quotes_gpc開啟$new_cart = unserialize($cart);如果magic_quotes_runtime是啟用的,那么在向文件中寫入序列化的數據之前必須用addslashes()進行處理,而在讀取它們之前則必須用stripslashes()進行處理:$fp = fopen('/tmp/cart','w');fputs($fp,addslashes(serialize($a)));fclose($fp);//如果magic_quotes_runtime開啟$new_cat = unserialize(stripslashes(file_get_contents('/tmp/cart')));//如果magic_quotes_runtime關閉$new_cat = unserialize(file_get_contents('/tmp/cart'));在啟用了magic_quotes_runtime的情況下,從數據庫中讀取序列化的數據也必須經過stripslashes()的處理,保存到數據庫中的序列化數據必須要經過addslashes()的處理,以便能夠適當地存儲。MySQL_query("insert into cart(id,data) values(1,'".addslashes(serialize($cart))."')");$rs = mysql_query('select data from cart where id=1');$ob = mysql_fetch_object($rs);//如果magic_quotes_runtime開啟$new_cart = unserialize(stripslashes($ob->data));//如果magic_quotes_runtime關閉$new_cart = unserialize($ob->data);
新聞熱點
疑難解答
圖片精選