Zend Framework入門教程之Zend_Session會話操作詳解
本文實(shí)例講述了Zend Framework入門教程之Zend_Session會話操作。分享給大家供大家參考,具體如下:
會話命名空間
實(shí)現(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
點(diǎn)評:
它會把這個(gè)對象所對應(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)解鎖!
點(diǎn)評:
由此可見,鎖定并不影響結(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í)它針對的是命名空間來設(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)容為:
點(diǎn)評:刷新兩次之后,就會有消失。之后陸續(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;
}
}
}
}
其實(shí)它還是基于PHP原始的Session來實(shí)現(xiàn)的。只是擴(kuò)展了部分功能。
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ù)中。
更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《Yii框架入門及常用技巧總結(jié)》、《ThinkPHP入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Zend Framework框架的PHP程序設(shè)計(jì)有所幫助。
- 工廠模式在Zend Framework中應(yīng)用介紹
- Zend Framework開發(fā)入門經(jīng)典教程
- Zend Framework入門之環(huán)境配置及第一個(gè)Hello World示例(附demo源碼下載)
- Zend Framework入門知識點(diǎn)小結(jié)
- Zend Framework 2.0事件管理器(The EventManager)入門教程
- Zend Framework入門教程之Zend_View組件用法示例
- Zend Framework入門教程之Zend_Registry組件用法詳解
- Zend Framework入門教程之Zend_Config組件用法詳解
- Zend Framework入門教程之Zend_Mail用法示例
- Zend Framework入門教程之Zend_Db數(shù)據(jù)庫操作詳解
- Zend Framework入門應(yīng)用實(shí)例詳解
相關(guān)文章
laravel使用數(shù)據(jù)庫測試注意事項(xiàng)
這篇文章主要介紹了laravel使用數(shù)據(jù)庫測試注意事項(xiàng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
ThinkPHP框架實(shí)現(xiàn)的郵箱激活功能示例
這篇文章主要介紹了ThinkPHP框架實(shí)現(xiàn)的郵箱激活功能,結(jié)合實(shí)例形式分析了thinkPHP使用class.smtp.php及class.phpmailer.php類文件進(jìn)行郵件發(fā)送實(shí)現(xiàn)激活功能的具體操作技巧,需要的朋友可以參考下2018-06-06
PHP實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)算法分享
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng)算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Yii2實(shí)現(xiàn)UploadedFile上傳文件示例
這篇文章主要介紹了Yii2實(shí)現(xiàn)UploadedFile上傳文件示例的資料,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-02-02

