


Detailed explanation of Zend_Session session operation in Zend Framework introductory tutorial
Jan 05, 2017 am 10:37 AM本文實例講述了Zend Framework入門教程之Zend_Session會話操作。分享給大家供大家參考,具體如下:
會話命名空間
實現(xiàn)會話
代碼:
<?php require_once "Zend/Session/Namespace.php"; $myNamespace = new Zend_Session_Namespace('Myspace'); if(isset($myNamespace->numberOfPageRequests)) { $myNamespace->numberOfPageRequests++; }else{ $myNamespace->numberOfPageRequests = 1; } echo "用戶的瀏覽次數(shù)為:"; echo "<font size=\"6\" color=\"#ff0000\">"; echo $myNamespace->numberOfPageRequests; echo "</font>次";
結(jié)果:
用戶的瀏覽次數(shù)為:10次
遍歷會話命名空間
代碼:
<?php require_once "Zend/Session/Namespace.php"; $myNamespace = new Zend_Session_Namespace('Myspace'); $myNamespace->webhost = "127.0.0.1"; $myNamespace->hostname = "localhost"; $myNamespace->user = "root"; $myNamespace->password = "123456"; $myNamespace->db_name = "test"; $myNamespace->db_type = "Sqlite"; foreach($myNamespace as $index=>$value){ echo "命名空間myNamespace中的:".$index; echo "為".$value."<p>\n"; }
結(jié)果:
命名空間myNamespace中的:webhost為127.0.0.1
命名空間myNamespace中的:hostname為localhost
命名空間myNamespace中的:user為root
命名空間myNamespace中的:password為123456
命名空間myNamespace中的:db_name為test
命名空間myNamespace中的:db_type為Sqlite
點評:
它會把這個對象所對應(yīng)空間中的所有內(nèi)容遍歷出來。很神奇。
訪問會話命名空間
代碼:
<?php require_once "Zend/Session/Namespace.php"; $login = new Zend_Session_Namespace('other'); $login->user = "Administrator"; if(isset($login->user)){ echo "\$login->user已經(jīng)有值,其值為:"; echo $login->user; unset($login->user); }else{ echo "\$login->user無值"; } echo "<p>"; if(isset($login->pass)){ echo "\$login->pass已經(jīng)有值,其值為:"; echo $login->pass; unset($login->pass); }else{ echo "\$login->pass無值"; } foreach($login as $index=>$value){ echo "命名空間login中的:".$index."為".$value."<p>\n"; }
結(jié)果:
$login->user已經(jīng)有值,其值為:Administrator
$login->pass無值
會話的高級應(yīng)用
開啟會話,有兩種方法
一、使用Zend_Session::start()開啟會話
二、new Zend_Session_Namespace()
鎖定會話命名空間
代碼:
<?php require_once "Zend/Session/Namespace.php"; $test = new Zend_Session_Namespace('test'); $test->name = "玉皇大帝"; $test->sex = "男"; $test->lock(); if($test->isLocked()){ echo "會話\$test已經(jīng)鎖定!<p>"; echo "命名空間\$test中的成員name的值為:"; echo $test->name; }else{ echo "會話\$test已經(jīng)解鎖!"; } echo "<p>"; $test->unLock(); if($test->isLocked()){ echo "會話\$test已經(jīng)鎖定!<p>"; echo "命名空間\$test中的成員name的值為:"; echo $test->name; }else{ echo "會話\$test已經(jīng)解鎖!"; }
結(jié)果:
會話$test已經(jīng)鎖定!
命名空間$test中的成員name的值為:玉皇大帝
會話$test已經(jīng)解鎖!
點評:
由此可見,鎖定并不影響結(jié)果的輸出。
分析源代碼
public function lock() { self::$_namespaceLocks[$this->_namespace] = true; } /** * unlock() - unmark a session/namespace to enable read & write * * @return void */ public function unlock() { unset(self::$_namespaceLocks[$this->_namespace]); } /** * unlockAll() - unmark all session/namespaces to enable read & write * * @return void */ public static function unlockAll() { self::$_namespaceLocks = array(); } /** * isLocked() - return lock status, true if, and only if, read-only * * @return bool */ public function isLocked() { return isset(self::$_namespaceLocks[$this->_namespace]); }
可知,它只是改變了參數(shù)而已。
為會話設(shè)置生命周期
setExpirationSeconds()方法與setExpirationHops()兩種方法來設(shè)置。
代碼:
<?php require_once "Zend/Session/Namespace.php"; $s = new Zend_Session_Namespace('temp'); $s->a = "蘋果"; $s->p = "梨"; $s->o = "桔子"; $s->setExpirationSeconds(60); $s->setExpirationHops(2,'a'); $s->setExpirationHops(3,'p'); echo "已經(jīng)為命名空間\$s設(shè)置生命期<p>";
設(shè)置生命期代碼,其實它針對的是命名空間來設(shè)置的。
測試代碼:
<?php require_once "Zend/Session/Namespace.php"; $b = new Zend_Session_Namespace('temp'); echo "\$b->a內(nèi)容為:".$b->a; echo "<p>"; echo "\$b->p內(nèi)容為:".$b->p;
先執(zhí)行設(shè)置生命期代碼,在執(zhí)行測試代碼會看到效果。
第一次:
$b->a內(nèi)容為:蘋果
$b->p內(nèi)容為:梨
第二次:
$b->a內(nèi)容為:蘋果
$b->p內(nèi)容為:梨
第三次:
$b->a內(nèi)容為:
$b->p內(nèi)容為:梨
第四次:
$b->a內(nèi)容為:
$b->p內(nèi)容為:
點評:刷新兩次之后,就會有消失。之后陸續(xù)消失。超過60秒效果相同。
分析源代碼,
public function setExpirationSeconds($seconds, $variables = null) { if (parent::$_writable === false) { /** * @see Zend_Session_Exception */ require_once 'Zend/Session/Exception.php'; throw new Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG); } if ($seconds <= 0) { /** * @see Zend_Session_Exception */ require_once 'Zend/Session/Exception.php'; throw new Zend_Session_Exception('Seconds must be positive.'); } if ($variables === null) { // apply expiration to entire namespace $_SESSION['__ZF'][$this->_namespace]['ENT'] = time() + $seconds; } else { if (is_string($variables)) { $variables = array($variables); } foreach ($variables as $variable) { if (!empty($variable)) { $_SESSION['__ZF'][$this->_namespace]['ENVT'][$variable] = time() + $seconds; } } } }
其實它還是基于PHP原始的Session來實現(xiàn)的。只是擴展了部分功能。
public function setExpirationHops($hops, $variables = null, $hopCountOnUsageOnly = false) { if (parent::$_writable === false) { /** * @see Zend_Session_Exception */ require_once 'Zend/Session/Exception.php'; throw new Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG); } if ($hops <= 0) { /** * @see Zend_Session_Exception */ require_once 'Zend/Session/Exception.php'; throw new Zend_Session_Exception('Hops must be positive number.'); } if ($variables === null) { // apply expiration to entire namespace if ($hopCountOnUsageOnly === false) { $_SESSION['__ZF'][$this->_namespace]['ENGH'] = $hops; } else { $_SESSION['__ZF'][$this->_namespace]['ENNH'] = $hops; } } else { if (is_string($variables)) { $variables = array($variables); } foreach ($variables as $variable) { if (!empty($variable)) { if ($hopCountOnUsageOnly === false) { $_SESSION['__ZF'][$this->_namespace]['ENVGH'][$variable] = $hops; } else { $_SESSION['__ZF'][$this->_namespace]['ENVNH'][$variable] = $hops; } } } } }
處理放在了構(gòu)造函數(shù)中。
希望本文所述對大家基于Zend Framework框架的PHP程序設(shè)計有所幫助。
更多Zend Framework入門教程之Zend_Session會話操作詳解相關(guān)文章請關(guān)注PHP中文網(wǎng)!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
