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:
1 2 3 4 |
|
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:
1 2 |
|
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:
1 2 |
|
If magic_quotes_runtime is enabled, serialized data must be processed with addslashes() before writing to the file, and stripslashes() before reading them:
1 2 3 4 5 6 7 |
|
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.
1 2 3 4 5 6 7 |
|
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)!