Detailed explanation of serialization and deserialization in php

黄舟
Release: 2023-03-06 12:14:01
Original
1338 people have browsed it

This article introduces the relevant knowledge of serialization and deserialization in php. It has a very good reference value. Let’s take a look at it with the editor.

Compress complex data types into a string

serialize() to combine variables and their The value is encoded into text form

unserialize() restores the original variable

eg:

$stooges = array('Moe','Larry','Curly');
$new = serialize($stooges);
print_r($new);echo "<br />";
print_r(unserialize($new));
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. When , you need to call urlencode() on this data to ensure that the URL metacharacters in it are processed:

$shopping = array(&#39;Poppy seed bagel&#39; => 2,&#39;Plain Bagel&#39; =>1,&#39;Lox&#39; =>4);
echo &#39;<a href="next.php?cart=&#39;.urlencode(serialize($shopping)).&#39;" rel="external nofollow" >next</a>&#39;;
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:

$new_cart = unserialize(stripslashes($cart)); //如果magic_quotes_gpc开启
$new_cart = unserialize($cart);
Copy after login

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

$fp = fopen(&#39;/tmp/cart&#39;,&#39;w&#39;);
fputs($fp,addslashes(serialize($a)));
fclose($fp);
//如果magic_quotes_runtime开启
$new_cat = unserialize(stripslashes(file_get_contents(&#39;/tmp/cart&#39;)));
//如果magic_quotes_runtime关闭
$new_cat = unserialize(file_get_contents(&#39;/tmp/cart&#39;));
Copy after login

When magic_quotes_runtime is enabled, the serialized data read from the database must also be processed by stripslashes() and saved to the serialization in the database. Data must be processed by addslashes() so that it can be stored appropriately.

mysql_query("insert into cart(id,data) values(1,&#39;".addslashes(serialize($cart))."&#39;)");
$rs = mysql_query(&#39;select data from cart where id=1&#39;);
$ob = mysql_fetch_object($rs);
//如果magic_quotes_runtime开启
$new_cart = unserialize(stripslashes($ob->data));
//如果magic_quotes_runtime关闭
$new_cart = unserialize($ob->data);
Copy after login

When deserializing an object, PHP will automatically call its __wakeUp() method. This allows the object to re-establish various states that were not preserved during serialization. For example: database connection, etc.

The above is the detailed explanation of serialization and deserialization in PHP. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!