Zend Framework入門(mén)教程之Zend_Session會(huì)話操作詳解
本文實(shí)例講述了Zend Framework入門(mén)教程之Zend_Session會(huì)話操作。分享給大家供大家參考,具體如下:
會(huì)話命名空間
實(shí)現(xiàn)會(huì)話
代碼:
<?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次
遍歷會(huì)話命名空間
代碼:
<?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)評(píng):
它會(huì)把這個(gè)對(duì)象所對(duì)應(yīng)空間中的所有內(nèi)容遍歷出來(lái)。很神奇。
訪問(wèn)會(huì)話命名空間
代碼:
<?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無(wú)值";
}
echo "<p>";
if(isset($login->pass)){
echo "\$login->pass已經(jīng)有值,其值為:";
echo $login->pass;
unset($login->pass);
}else{
echo "\$login->pass無(wú)值";
}
foreach($login as $index=>$value){
echo "命名空間login中的:".$index."為".$value."<p>\n";
}
結(jié)果:
$login->user已經(jīng)有值,其值為:Administrator
$login->pass無(wú)值
會(huì)話的高級(jí)應(yīng)用
開(kāi)啟會(huì)話,有兩種方法
一、使用Zend_Session::start()開(kāi)啟會(huì)話
二、new Zend_Session_Namespace()
鎖定會(huì)話命名空間
代碼:
<?php
require_once "Zend/Session/Namespace.php";
$test = new Zend_Session_Namespace('test');
$test->name = "玉皇大帝";
$test->sex = "男";
$test->lock();
if($test->isLocked()){
echo "會(huì)話\$test已經(jīng)鎖定!<p>";
echo "命名空間\$test中的成員name的值為:";
echo $test->name;
}else{
echo "會(huì)話\$test已經(jīng)解鎖!";
}
echo "<p>";
$test->unLock();
if($test->isLocked()){
echo "會(huì)話\$test已經(jīng)鎖定!<p>";
echo "命名空間\$test中的成員name的值為:";
echo $test->name;
}else{
echo "會(huì)話\$test已經(jīng)解鎖!";
}
結(jié)果:
會(huì)話$test已經(jīng)鎖定!
命名空間$test中的成員name的值為:玉皇大帝
會(huì)話$test已經(jīng)解鎖!
點(diǎn)評(píng):
由此可見(jià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ù)而已。
為會(huì)話設(shè)置生命周期
setExpirationSeconds()方法與setExpirationHops()兩種方法來(lái)設(shè)置。
代碼:
<?php
require_once "Zend/Session/Namespace.php";
$s = new Zend_Session_Namespace('temp');
$s->a = "蘋(píng)果";
$s->p = "梨";
$s->o = "桔子";
$s->setExpirationSeconds(60);
$s->setExpirationHops(2,'a');
$s->setExpirationHops(3,'p');
echo "已經(jīng)為命名空間\$s設(shè)置生命期<p>";
設(shè)置生命期代碼,其實(shí)它針對(duì)的是命名空間來(lái)設(shè)置的。
測(cè)試代碼:
<?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í)行測(cè)試代碼會(huì)看到效果。
第一次:
$b->a內(nèi)容為:蘋(píng)果
$b->p內(nèi)容為:梨
第二次:
$b->a內(nèi)容為:蘋(píng)果
$b->p內(nèi)容為:梨
第三次:
$b->a內(nèi)容為:
$b->p內(nèi)容為:梨
第四次:
$b->a內(nèi)容為:
$b->p內(nèi)容為:
點(diǎn)評(píng):刷新兩次之后,就會(huì)有消失。之后陸續(xù)消失。超過(guò)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來(lái)實(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)容感興趣的讀者可查看本站專(zhuān)題:《Zend FrameWork框架入門(mén)教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《Yii框架入門(mén)及常用技巧總結(jié)》、《ThinkPHP入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Zend Framework框架的PHP程序設(shè)計(jì)有所幫助。
- 工廠模式在Zend Framework中應(yīng)用介紹
- Zend Framework開(kāi)發(fā)入門(mén)經(jīng)典教程
- Zend Framework入門(mén)之環(huán)境配置及第一個(gè)Hello World示例(附demo源碼下載)
- Zend Framework入門(mén)知識(shí)點(diǎn)小結(jié)
- Zend Framework 2.0事件管理器(The EventManager)入門(mén)教程
- Zend Framework入門(mén)教程之Zend_View組件用法示例
- Zend Framework入門(mén)教程之Zend_Registry組件用法詳解
- Zend Framework入門(mén)教程之Zend_Config組件用法詳解
- Zend Framework入門(mén)教程之Zend_Mail用法示例
- Zend Framework入門(mén)教程之Zend_Db數(shù)據(jù)庫(kù)操作詳解
- Zend Framework入門(mén)應(yīng)用實(shí)例詳解
相關(guān)文章
PHP與Web頁(yè)面交互例子的實(shí)現(xiàn)
本文主要介紹了PHP?與?Web?頁(yè)面交互例子的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
使用CodeIgniter的類(lèi)庫(kù)做圖片上傳
CodeIgniter的文件上傳類(lèi)允許文件被上傳。您可以設(shè)置指定上傳某類(lèi)型的文件及指定大小的文件。這篇文章主要介紹了使用CodeIgniter的類(lèi)庫(kù)做圖片上傳,需要的朋友可以參考下2014-06-06
Yii實(shí)現(xiàn)單用戶博客系統(tǒng)文章詳情頁(yè)插入評(píng)論表單的方法
這篇文章主要介紹了Yii實(shí)現(xiàn)單用戶博客系統(tǒng)文章詳情頁(yè)插入評(píng)論表單的方法,結(jié)合實(shí)例分析了Yii實(shí)現(xiàn)文章詳情頁(yè)評(píng)論表單功能的具體技巧,需要的朋友可以參考下2015-12-12
php實(shí)現(xiàn)姓名根據(jù)首字母排序的類(lèi)與方法(實(shí)例代碼)
這篇文章主要介紹了php實(shí)現(xiàn)姓名根據(jù)首字母排序的類(lèi)與方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05
destoon實(shí)現(xiàn)公司新聞詳細(xì)頁(yè)添加評(píng)論功能的方法
這篇文章主要介紹了destoon實(shí)現(xiàn)公司新聞詳細(xì)頁(yè)添加評(píng)論功能的方法,需要的朋友可以參考下2014-07-07

