


本文实例讲述了Zend Framework入门教程之Zend_Session会话操作。分享给大家供大家参考,具体如下:
会话命名空间
实现会话
代码:
<?php require_once "Zend/Session/Namespace.php"; $myNamespace = new Zend_Session_Namespace('Myspace'); if(isset($myNamespace->numberOfPageRequests)) { $myNamespace->numberOfPageRequests++; }else{ $myNamespace->numberOfPageRequests = 1; } echo "用户的浏览次数为:"; echo "<font size=\"6\" color=\"#ff0000\">"; echo $myNamespace->numberOfPageRequests; echo "</font>次";
结果:
用户的浏览次数为: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"; }
结果:
命名空间myNamespace中的:webhost为127.0.0.1
命名空间myNamespace中的:hostname为localhost
命名空间myNamespace中的:user为root
命名空间myNamespace中的:password为123456
命名空间myNamespace中的:db_name为test
命名空间myNamespace中的:db_type为Sqlite
点评:
它会把这个对象所对应空间中的所有内容遍历出来。很神奇。
访问会话命名空间
代码:
<?php require_once "Zend/Session/Namespace.php"; $login = new Zend_Session_Namespace('other'); $login->user = "Administrator"; if(isset($login->user)){ echo "\$login->user已经有值,其值为:"; echo $login->user; unset($login->user); }else{ echo "\$login->user无值"; } echo "<p>"; if(isset($login->pass)){ echo "\$login->pass已经有值,其值为:"; echo $login->pass; unset($login->pass); }else{ echo "\$login->pass无值"; } foreach($login as $index=>$value){ echo "命名空间login中的:".$index."为".$value."<p>\n"; }
结果:
$login->user已经有值,其值为:Administrator
$login->pass无值
会话的高级应用
开启会话,有两种方法
一、使用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已经锁定!<p>"; echo "命名空间\$test中的成员name的值为:"; echo $test->name; }else{ echo "会话\$test已经解锁!"; } echo "<p>"; $test->unLock(); if($test->isLocked()){ echo "会话\$test已经锁定!<p>"; echo "命名空间\$test中的成员name的值为:"; echo $test->name; }else{ echo "会话\$test已经解锁!"; }
结果:
会话$test已经锁定!
命名空间$test中的成员name的值为:玉皇大帝
会话$test已经解锁!
点评:
由此可见,锁定并不影响结果的输出。
分析源代码
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]); }
可知,它只是改变了参数而已。
为会话设置生命周期
setExpirationSeconds()方法与setExpirationHops()两种方法来设置。
代码:
<?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 "已经为命名空间\$s设置生命期<p>";
设置生命期代码,其实它针对的是命名空间来设置的。
测试代码:
<?php require_once "Zend/Session/Namespace.php"; $b = new Zend_Session_Namespace('temp'); echo "\$b->a内容为:".$b->a; echo "<p>"; echo "\$b->p内容为:".$b->p;
先执行设置生命期代码,在执行测试代码会看到效果。
第一次:
$b->a内容为:苹果
$b->p内容为:梨
第二次:
$b->a内容为:苹果
$b->p内容为:梨
第三次:
$b->a内容为:
$b->p内容为:梨
第四次:
$b->a内容为:
$b->p内容为:
点评:刷新两次之后,就会有消失。之后陆续消失。超过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来实现的。只是扩展了部分功能。
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; } } } } }
处理放在了构造函数中。
希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。
更多Zend Framework入门教程之Zend_Session会话操作详解相关文章请关注PHP中文网!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
