<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[php中序列化与反序列化]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[代码编程]]></category>
<pubDate>Sat, 06 Jul 2013 22:37:11 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	把复杂的数据类型压缩到一个字符串中<br/><br/>serialize() 把变量和它们的值编码成文本形式<br/>unserialize() 恢复原先变量<br/><br/><div class="code"><br/>$stooges = array(&#039;Moe&#039;,&#039;Larry&#039;,&#039;Curly&#039;);<br/>$new = serialize($stooges);<br/>print_r($new);echo &quot;&lt;br /&gt;&quot;;<br/>print_r(unserialize($new));<br/></div><br/><br/>结果：a:3:&#123;i:0;s:3:"Moe";i:1;s:5:"Larry";i:2;s:5:"Curly";&#125;<br/>Array ( [0] => Moe [1] => Larry [2] => Curly )<br/>当把这些序列化的数据放在URL中在页面之间会传递时，需要对这些数据调用urlencode()，以确保在其中的URL元字符进行处理：<br/><div class="code"><br/>$shopping = array(&#039;Poppy seed bagel&#039; =&gt; 2,&#039;Plain Bagel&#039; =&gt;1,&#039;Lox&#039; =&gt;4);<br/>echo &#039;&lt;a href=&quot;next.php?cart=&#039;.urlencode(serialize($shopping)).&#039;&quot;&gt;next&lt;/a&gt;&#039;;<br/></div><br/><br/>margic_quotes_gpc和magic_quotes_runtime配置项的设置会影响传递到unserialize()中的数据。<br/>如果magic_quotes_gpc项是启用的，那么在URL、POST变量以及cookies中传递的数据在反序列化之前必须用stripslashes()进行处理：<br/><br/><div class="code"><br/>$new_cart = unserialize(stripslashes($cart)); //如果magic_quotes_gpc开启<br/>$new_cart = unserialize($cart);<br/></div><br/><br/>如果magic_quotes_runtime是启用的，那么在向文件中写入序列化的数据之前必须用addslashes()进行处理，而在读取它们之前则必须用stripslashes()进行处理：<br/><div class="code"><br/>$fp = fopen(&#039;/tmp/cart&#039;,&#039;w&#039;);<br/>fputs($fp,addslashes(serialize($a)));<br/>fclose($fp);<br/>//如果magic_quotes_runtime开启<br/>$new_cat = unserialize(stripslashes(file_get_contents(&#039;/tmp/cart&#039;)));<br/>//如果magic_quotes_runtime关闭<br/>$new_cat = unserialize(file_get_contents(&#039;/tmp/cart&#039;));<br/>在启用了magic_quotes_runtime的情况下，从数据库中读取序列化的数据也必须经过stripslashes()的处理，保存到数据库中的序列化数据必须要经过addslashes()的处理，以便能够适当地存储。<br/>mysql_query(&quot;insert into cart(id,data) values(1,&#039;&quot;.addslashes(serialize($cart)).&quot;&#039;)&quot;);<br/>$rs = mysql_query(&#039;select data from cart where id=1&#039;);<br/>$ob = mysql_fetch_object($rs);<br/>//如果magic_quotes_runtime开启<br/>$new_cart = unserialize(stripslashes($ob-&gt;data));<br/>//如果magic_quotes_runtime关闭<br/>$new_cart = unserialize($ob-&gt;data);<br/></div><br/><br/>当对一个对象进行反序列化操作时，PHP会自动地调用其__wakeUp()方法。这样就使得对象能够重新建立起序列化时未能保留的各种状态。例如：数据库连接等。<br/>Tags - <a href="http://www.dzhope.com/tags/%25E5%25BA%258F%25E5%2588%2597%25E5%258C%2596/" rel="tag">序列化</a> , <a href="http://www.dzhope.com/tags/%25E5%258F%258D%25E5%25BA%258F%25E5%2588%2597%25E5%258C%2596/" rel="tag">反序列化</a>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] php中序列化与反序列化]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>