Home > Backend Development > PHP Tutorial > In-depth understanding of serialization and deserialization in php

In-depth understanding of serialization and deserialization in php

迷茫
Release: 2023-03-06 22:50:01
Original
1410 people have browsed it

Compress complex data types into a string

serialize() Encode variables and their values ​​into text form

unserialize() Restore original variables

<span style="color: #800080">$stooges = <span style="color: #0000ff"><a href="//m.sbmmt.com/wiki/1041.html" target="_blank">array</a>(&#39;Moe&#39;,&#39;Larry&#39;,&#39;Curly&#39;);<br/><span style="color: #800080">$<a href="//m.sbmmt.com/wiki/165.html" target="_blank">new</a> = <span style="color: #008080">serialize(<span style="color: #800080">$stooges);<br/><span style="color: #008080"><a href="//m.sbmmt.com/wiki/1362.html" target="_blank">print</a>_r(<span style="color: #800080">$new);<span style="color: #0000ff"><a href="//m.sbmmt.com/wiki/1343.html" target="_blank">echo</a> "<br />";<br/><span style="color: #008080">print_r(<span style="color: #008080">unserialize(<span style="color: #800080">$new));</span></span></span></span></span></span></span></span></span></span></span>
Copy after login

Result: a:3:{i:0;s:3:"Moe";i:1;s:5:"Larry";i:2;s:5:"Curly";}

Array ([0] => Moe [1] => Larry [2] => Curly )

When these serialized data are placed in the URL, they will be passed between pages. , you need to call urlencode() on this data to ensure that the URL metacharacters in it are processed:

<span style="color: #800080">$shopping = <span style="color: #0000ff">array(&#39;Poppy seed bagel&#39; => 2,&#39;Plain Bagel&#39; =>1,&#39;Lox&#39; =>4);<br/><span style="color: #0000ff">echo &#39;<a href="next.php?cart=&#39;.<span style="color: #008080">urlencode(<span style="color: #008080">serialize(<span style="color: #800080">$shopping)).&#39;">next</a>&#39;;</span></span></span></span></span></span>
Copy after login

The settings of the margic_quotes_gpc and magic_quotes_runtime configuration items will affect the data passed to unserialize().

If the magic_quotes_gpc item is enabled, data passed in URLs, POST variables, and cookies must be processed with stripslashes() before deserialization:

<span style="color: #800080">$new_cart = <span style="color: #008080">unserialize(<span style="color: #008080">stripslashes(<span style="color: #800080">$cart)); <span style="color: #008000">//<span style="color: #008000">如果magic_quotes_gpc开启<span style="color: #008000"><br/><span style="color: #800080">$new_cart = <span style="color: #008080">unserialize(<span style="color: #800080">$cart);</span></span></span></span></span></span></span></span></span></span>
Copy after login

If magic_quotes_runtime is enabled , then serialized data must be processed with addslashes() before writing to the file, and stripslashes() before reading them:

//当对一个对象进行反序列化操作时,PHP会自动地调用其wakeUp()方法。这样就使得对象能够重新建立起序列化时未能保留的各种状态。例如:数据库连接等。
Copy after login

The above is the detailed content of In-depth understanding of serialization and deserialization in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template