Home Backend Development PHP Tutorial PHP串行化(序列化)跟反串行化

PHP串行化(序列化)跟反串行化

Jun 13, 2016 am 11:12 AM
data index name quot unserialize

PHP串行化(序列化)和反串行化

这个和java的序列话是一样的。只是java要实现Serializable这个空接口。


serialize() 把变量和它们的值编码成文本形式

unserialize() 恢复原先变量

什么情况下需要序列化 当你想把的内存中的对象写入到硬盘 数据库的时候;当你想在网络上传送对象的时候;
Copy after login

当把这些序列化的数据放在URL中在页面之间会传递时,需要对这些数据调用urlencode(),以确保在其中的URL元字符进行处理

margic_quotes_gpc和magic_quotes_runtime配置项的设置会影响传递到unserialize()中的数据。
如果magic_quotes_gpc项是启用的,那么在URL、POST变量以及cookies中传递的数据在反序列化之前必须用stripslashes()进行处理:如果magic_quotes_runtime是启用的,那么在向文件中写入序列化的数据之前必须用addslashes()进行处理,而在读取它们之前则必须用stripslashes()进行处理:


也可用array,把一个数组对象系列化。

<?php class Data{		var $index;		var $name;				function __construct($index,$name){			$this->index = $index;			$this->name = $name;		}	}		$data1 = new Data(1, "hello");	$data2 = new Data(2, "world");	$arr = array();	//用ArrayObject也可以。	//$arr = new ArrayObject();	$arr[0] = $data1;	$arr[1] = $data2;	$str = serialize($arr);		$file = fopen("tmp.txt", "w");	fwrite($file, $str);	fclose($file);	//$file =fopen("tmp.txt", "r");	$data = file_get_contents("tmp.txt");		//反序列化得到原来的数组对象。	$obj = unserialize($data);	print_r($obj[0]);	echo $obj[0]->name;	?>
Copy after login

tmp.txt的内容为:

a:2:{i:0;O:4:"Data":2:{s:5:"index";i:1;s:4:"name";s:5:"hello";}i:1;O:4:"Data":2:{s:5:"index";i:2;s:4:"name";s:5:"world";}}
Copy after login


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What file is index.html? What file is index.html? Feb 19, 2024 pm 01:36 PM

index.html represents the home page file of the web page and is the default page of the website. When a user visits a website, the index.html page is usually loaded first. HTML (HypertextMarkupLanguage) is a markup language used to create web pages, and index.html is also an HTML file. It contains the structure and content of a web page, as well as tags and elements used for formatting and layout. Here is an example index.html code: &lt

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Use PHP unserialize() function to implement deserialization Use PHP unserialize() function to implement deserialization Jun 27, 2023 am 08:01 AM

Serialization is the process of converting a data structure or object into a string for storage, transmission, or representation, and conversely, parsing a string into the original data structure or object. In PHP, we can use the serialize() function to serialize a variable into a string, and use the unserialize() function to deserialize a string into a primitive data structure or object. This article will focus on the use and precautions of the PHPunserialize() function. 1. unserialize

What data is in the data folder? What data is in the data folder? May 05, 2023 pm 04:30 PM

The data folder contains system and program data, such as software settings and installation packages. Each folder in the Data folder represents a different type of data storage folder, regardless of whether the Data file refers to the file name Data or the extension. Named data, they are all data files customized by the system or program. Data is a backup file for data storage. Generally, it can be opened with meidaplayer, notepad or word.

what is mysql index what is mysql index Oct 08, 2023 am 11:47 AM

The index in MySQL means index. It is a data structure used to speed up the query of database tables. The index can be compared to the catalog of a book. It stores the values ​​​​of specific columns in the table and the corresponding row positions, making the database more efficient. Locate and access data quickly. The function of the index is to improve query efficiency. Without an index, the database needs to scan the entire table row by row to find matching data. This method will be very time-consuming in large tables. With an index, the database can The required data rows are quickly located in the order, which greatly improves the query speed.

A brief analysis of the usage of serialize and unserialize in php A brief analysis of the usage of serialize and unserialize in php Mar 24, 2023 pm 02:57 PM

PHP is a popular programming language commonly used for web development. Among them, serialize and unserialize are two very useful functions that can convert PHP objects into strings and deserialize them.

What to do if mysql load data is garbled? What to do if mysql load data is garbled? Feb 16, 2023 am 10:37 AM

The solution to the garbled mysql load data: 1. Find the SQL statement with garbled characters; 2. Modify the statement to "LOAD DATA LOCAL INFILE "employee.txt" INTO TABLE EMPLOYEE character set utf8;".

PHP source code running problem: index error solution PHP source code running problem: index error solution Mar 09, 2024 pm 09:24 PM

PHP source code running problem: Index error resolution requires specific code examples. PHP is a widely used server-side scripting language that is often used to develop dynamic websites and web applications. However, sometimes you will encounter various problems when running PHP source code, among which "index error" is a common situation. This article will introduce some common causes and solutions of index errors, and provide specific code examples to help readers better deal with such problems. Problem Description: When running a PHP program

See all articles