php序列化數(shù)據(jù) php對象序列化

php數(shù)組序列化a:1:{i:0;s:1:"1"} 的詳細解釋

1、首先創(chuàng)建好一個數(shù)組然后序列化,array("table" = member, "field" = 1,"rule" = -0 ,"cycle" = 24 ,"max" = 1 )。

成都創(chuàng)新互聯(lián)是一家專業(yè)提供雞東企業(yè)網(wǎng)站建設,專注與網(wǎng)站設計、成都網(wǎng)站建設、HTML5、小程序制作等業(yè)務。10年已為雞東眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡公司優(yōu)惠進行中。

2、然后用serilize函數(shù)序列號,接著在打印輸出看序列化的結果。

3、結果{s:5:"table";s:6:"member";s:5:"field";i:1;s:4:"rule";i:0;s:5:"cycle";i:24;s:3:"max";i:},a:后代表是個數(shù),s:后代表是字符串長度,i:是數(shù)字長度。

4、然后將序列化字符串,輸入到mysql數(shù)據(jù)庫中以備使用。

5、最后將反序列化的結果打印,會顯示出一個數(shù)組,就完成了。

php中序列化與反序列化

把復雜的數(shù)據(jù)類型壓縮到一個字符串中

serialize() 把變量和它們的值編碼成文本形式

unserialize() 恢復原先變量

eg:

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

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

當把這些序列化的數(shù)據(jù)放在URL中在頁面之間會傳遞時,需要對這些數(shù)據(jù)調(diào)用urlencode(),以確保在其中的URL元字符進行處理:

margic_quotes_gpc和magic_quotes_runtime配置項的設置會影響傳遞到unserialize()中的數(shù)據(jù)。

如果magic_quotes_gpc項是啟用的,那么在URL、POST變量以及cookies中傳遞的數(shù)據(jù)在反序列化之前必須用stripslashes()進行處理:

如果magic_quotes_runtime是啟用的,那么在向文件中寫入序列化的數(shù)據(jù)之前必須用addslashes()進行處理,而在讀取它們之前則必須用stripslashes()進行處理:

當對一個對象進行反序列化操作時,PHP會自動地調(diào)用其__wakeUp()方法。這樣就使得對象能夠重新建立起序列化時未能保留的各種狀態(tài)。例如:數(shù)據(jù)庫連接等。

簡單分析PHP中序列化用法介紹

簡單分析PHP中序列化用法介紹

序列化在我們學習php中都會有用到了對于序列化我們常用的函數(shù)有serialize和unserialize了,希望以下這篇文章能夠幫助到各位了解到PHP中序列化用法,具體如下:

0x00 序列化函數(shù)

serialize():返回帶有變量類型和值的字符串

unserialize():想要將已序列化的字符串變回 PHP 的值

測試代碼:

?php

class test{

var $a;

var $b;

function __construct($a,$b,$c){

$a = $a;

$this-b = $b;

}

}

class test1 extends test{

function __construct($a){

$this-a = $a;

}

}

$a = 'hello';

$b = 123;

$c = false;

$d = new test('helloa','hellob','helloc');

$e = new test1('hello');

var_dump(serialize($a));

var_dump(serialize($b));

var_dump(serialize($c));

var_dump(serialize($d));

var_dump(serialize($e));

?

運行結果:

string 's:5:"hello";' (length=12)

string 'i:123;' (length=6)

string 'b:0;' (length=4)

string 'O:4:"test":2:{s:1:"a";N;s:1:"b";s:6:"hellob";}' (length=46)

string 'O:5:"test1":2:{s:1:"a";s:5:"hello";s:1:"b";N;}' (length=46)

序列化字符串格式: 變量類型:變量長度:變量內(nèi)容 。

如果序列化的是一個對象,序列化字符串格式為:

變量類型:類名長度:類名:屬性數(shù)量:{屬性類型:屬性名長度:屬性名;屬性值類型:屬性值長度:屬性值內(nèi)容}

將上述結果反序列化輸出,執(zhí)行結果:

string 'hello' (length=5)

int 123

boolean false

object(test)[1]

public 'a' = null

public 'b' = string 'hellob' (length=6)

object(test1)[1]

public 'a' = string 'hello' (length=5)

public 'b' = null

0x01 對象序列化

當序列化對象時,PHP 將在序列動作之前調(diào)用該對象的成員函數(shù) sleep()。這樣就允許對象在被序列化之前做任何清除操作。類似的,當使用 unserialize() 恢復對象時, 將調(diào)用 wakeup()成員函數(shù)。

在serialize()函數(shù)執(zhí)行時,會先檢查類中是否定義了 sleep()函數(shù),如果存在,則首先調(diào)用 sleep()函數(shù),如果不存在,就保留序列字符串中的所有屬性。

在unserialize()函數(shù)執(zhí)行時,會先檢查是否定義了 wakeup()函數(shù)。如果 wakeup()存在,將執(zhí)行__wakeup()函數(shù),會使變量被重新賦值。

serialize()測試代碼:

?php

class test{

var $a;

var $b;

function __construct($a,$b,$c){

$this-a = $a;

$this-b = $b;

}

function __sleep(){

echo "b has changed"."\n";

$this-b = 'hib';

return $this-b;

}

function __wakeup(){

echo "a has changed"."\n";

$this-a = 'hia';

}

}

class test1 extends test{

function __construct($a){

$this-a = $a;

}

}

$d = new test('helloa','hellob','helloc');

$e = new test1('hello');

serialize($d);

serialize($e);

var_dump($d);

var_dump($e);

?

執(zhí)行結果:

b has changed b has changed

object(test)[1]

public 'a' = string 'helloa' (length=6)

public 'b' = string 'hib' (length=3)

object(test1)[2]

public 'a' = string 'hello' (length=5)

public 'b' = string 'hib' (length=3)

unserialize()測試代碼:

class test{

var $a;

var $b;

function __construct($a,$b,$c){

$this-a = $a;

$this-b = $b;

}

function __sleep(){

echo "b has changed"."\n";

$this-b = 'hib';

return $this-b;

}

function __wakeup(){

echo "a has changed"."\n";

$this-a = 'hia';

}

}

class test1 extends test{

function __construct($a){

$this-a = $a;

}

}

$d = 'O:4:"test":2:{s:1:"a";N;s:1:"b";s:6:"hellob";}' ;

php 序列化的數(shù)據(jù)存儲到什么地方

在PHP中使用serialize()序列化數(shù)據(jù)是非常便捷的。serialize()返回一個字符串,此字符串包含了表示數(shù)據(jù)的字節(jié)流。使用serialize()產(chǎn)生的這個字符串可以存儲于任何地方,同時不會丟失其數(shù)據(jù)類型和結構,這有利于存儲或傳遞PHP的值。

serialize()可以處理除resource之外的任何類型,甚至可以處理那些包含了指向其自身引用的數(shù)組。數(shù)組/對象中的引用也將被存儲,并且自PHP4版本以后可以同時存儲對象的屬性和方法(PHP3只能存儲對象的屬性)。

但是在使用serialize()的時候一定要注意對其內(nèi)容先進行addslashes()處理,否則序列化的數(shù)據(jù)中如果有特殊字符就會導致字符串無法使用unserialize()進行反序列化。

php 什么是序列化

序列化是將變量轉換為可保存或傳輸?shù)淖址倪^程;反序列化就是在適當?shù)臅r候把這個字符串再轉化成原來的變量使用。這兩個過程結合起來,可以輕松地存儲和傳輸數(shù)據(jù),使程序更具維護性。

PHP中的序列化和反序列化分別通過函數(shù)serialize()和unserialize()即可實現(xiàn)。serialize()的參數(shù)可以是resource類型外的所有變量類型,最常見的是用來序列化對象,unseialize()將serialize的返回結果作為參數(shù),進行反序列化,得到原對象。

本文標題:php序列化數(shù)據(jù) php對象序列化
文章URL:http://muchs.cn/article4/hhecie.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、動態(tài)網(wǎng)站、品牌網(wǎng)站建設網(wǎng)站建設、云服務器、網(wǎng)站導航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司