對象串行化
Jun 08, 2016 pm 05:33 PM串行化可以把變量包括對象,轉化成連續(xù)bytes數(shù)據(jù). 你可以將串行化后的變量存在一個文件里或在網(wǎng)絡上傳輸. 然后再反串行化還原為原來的數(shù)據(jù). 你在反串行化類的對象之前定義的類,PHP可以成功地存儲其對象的屬性和方法. 有時你可能需要一個對象在反串行化后立即執(zhí)行. 為了這樣的目的,PHP會自動尋找__sleep和__wakeup方法.
當一個對象被串行化,PHP會調(diào)用__sleep方法(假如存在的話). 在反串行化一個對象后,PHP 會調(diào)用__wakeup方法. 這兩個方法都不接受參數(shù). __sleep方法必須返回一個數(shù)組,包含需要串行化的屬性. PHP會拋棄其它屬性的值. 假如沒有__sleep方法,PHP將保存所有屬性.
例子6.16顯示了如何用__sleep和__wakeup方法來串行化一個對象. Id屬性是一個不打算保留在對象中的臨時屬性. __sleep方法保證在串行化的對象中不包含id屬性. 當反串行化一個User對象,__wakeup方法建立id屬性的新值. 這個例子被設計成自我保持. 在實際開發(fā)中,你可能發(fā)現(xiàn)包含資源(如圖像或數(shù)據(jù)流)的對象需要這些方法.
Listing 6.16 Object serialization
class User
{
public $name;
public $id;
function __construct()
{
//give user a unique ID 賦予一個不同的ID
$this->id = uniqid();
}
function __sleep()
{
//do not serialize this->id 不串行化id
return(array("name"));
}
function __wakeup()
{
//give user a unique ID
$this->id = uniqid();
}
}
//create object 建立一個對象
$u = new User;
$u->name = "Leon";
//serialize it 串行化 注重不串行化id屬性,id的值被拋棄
$s = serialize($u);
//unserialize it 反串行化 id被重新賦值
$u2 = unserialize($s);
//$u and $u2 have different IDs $u和$u2有不同的ID
print_r($u);
print_r($u2);
?>

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











PHP ??? ??????? ?? ??? ??? ??? ??? ?? ??? ?? ??? ????. ??? ?? ?????. 1. ?? PHP ?? ? ???? ??? ???? ?? ? ?? phpinfo.php ??? ???? ? ? ????. 2. ??? ? ??? ???? ??????. 8.2 ?? 8.1? ???? ?? ????. Linux ???? Package Manager? ???? MacOS ???? Homebrew? ?????. 3. ?? ?? ? ???? ???????? php.ini? ?????? ??? ??? ??????. 4. ? ???? ????? ?? ??? ????? ?? ??? ???? ??? ??? ??? ??????. ?? ??? ??? ???? ???? ?????? ????? ?? ? ? ????.

TopreventCSRFattacksinPHP,implementanti-CSRFtokens.1)Generateandstoresecuretokensusingrandom_bytes()orbin2hex(random_bytes(32)),savethemin$_SESSION,andincludetheminformsashiddeninputs.2)ValidatetokensonsubmissionbystrictlycomparingthePOSTtokenwiththe

PHP ?? ??? ????? ??? ??? ???? ??? ???? ???????. ?? ???? PHP ?? ???? ? ?? ?? ??? ?????. ? ?? (Apache ?? Nginx), PHP ?? ? ?????? (? : MySQL/MariaDB); ???? XAMPP ?? MAMP? ?? ?? ???? ???? ?? ????? ????? ?? ????. XAMPP? Windows ? MacOS? ?????. ?? ? ???? ??? HTDOCS ????? ???? LocalHost? ?? ??????. mAMP? MAC ????? ???? PHP ??? ??? ??? ????? ?? ???? ??? ???? ????. ④ ?? ???? MacOS/Linux ????? Homebrew? ???? ??? ? ????.

? ?? PHP ??? ???? ?? ? ?? ????? ? ?? ?? ??? ????. 1. ??? ??? ?? ?? ??? ?? Array_Merge ? Array_unique ??? ??????. ?? Array_Merge ($ array1, $ array2)? ?? ? ?? Array_Unique ()? ???? ?? ?? ? ?? ???? ? ??? ?????. 2. ?? ?? ? ? ?? ???? ? ? ?? ????? ???? ??????. $ result = $ array1 $ array2? ? ?? ??? ?? ? ?? ??? ?? ?? ?? ??????. ? ? ?? ??? ? ??? ????? ??? ?? ?

EXIT ()? ???? ??? ?? ???? ? ???? PHP? ?????. ???? ???? ??? ?????. 1. ??? ???? ??? ??? ???? ?? ?? ??? ?? ? ? ????? ?? ?????. 2. ??? ? ?? ?? ?? ? ?? ??; 3. ?? ?? ??? ???? ?? Header ()? ?? ???? ? Exit ()? ??????. ?? exit ()? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ? ? ??? ??? ?? ()???.

HTML?? ??? ??? ????? ???? ??? ?? ???, ??? ? SEO ??? ?? ? ? ????. 1. ??? ????? ??? ?? ??? ? ??? ??? ????? ?? ????????. 2. ????? ??? ???? ?? ?? ???? ???? ???? ?? ??? ?????. 3. ? ???? ??? ?? ??? ????? ??? ? ?? ?? ?? ?? ????? ?? ??? ????. ?? ???? ??? ??????? ?? ??? ??? ??? ???? ???? ??? ??? ?? ??? ???? ???????.

PHP? ?? ???? ?????? ?? ??? ??? ?? $ _session hyperglobal ???? ?? ???????. 1. ??? session_start ()? ???? ??????, ?? ?? ??? ???????. 2. ?? ???? ??? ? ? ?? ????? ??????. ISSET ($ _ session [ 'key']) ?? array_key_exists ( 'key', $ _ session)? ??? ? ????. 3. ?? ??? ????? ????????. ?? ???? $ _session ??? ?? ???????. 4. UNSET ($ _ session [ 'key'])? ?? ???? ??? ?? ???? ??? $ _session? ? ??? ??????.

?? ??? PHP? ?? ? ??? ?????. ?? ??? 1???. ?? ??? ???? (?? ?), 2. ??? ???? ?? ??? ???? (?? ?). ?? ? ??? ????, ?? ?? ??? ?????, ?? ?? ??, ???? ???? ?? ?? ?? ??? ??? ?????, ??? ?? ? ?? ?? ???? ?????? ???????. ?? ?? ?? ??? ??????? ?? ??? ?? ?????? ?? ?? ??? ????? ?? ??? ????????. ?? ??, ????? ?? ? ? ??? ?? ????? ???? ?? ??? ?? ? ??? ?? ??? ?????.
