[参看ZendFramework官方文档] php
(1)开启全局会话,并设置session session
<?php require_once 'Zend/Session.php'; require_once 'Zend/Session/Namespace.php'; Zend_Session::start(); $userProfileNamespace = new Zend_Session_Namespace('userProfileNamespace'); $userProfileNamespace->name='username'; $userProfileNamespace->setExpirationSeconds(60, 'name'); // session有效期1分钟 $s->setExpirationHops(1); // 1次访问后,会话过时 // 标记会话设置为只读锁定 $userProfileNamespace->lock(); ?>
注意,不能使用不要直接使用PHP的session_start()函数。若是你直接使用session_start(),以后再使用Zend_Session_Namespace,那么Zend_Session::start()会抛出("会话已经开始")的异常。若是你在使用Zend_Session_Namespace或使用Zend_Session::start()后调用session_start(),那么会产生一个E_NOTICE级别的错误,且该调用将会被忽略。 函数
(2)使用(1)中的name ui
<?php $userProfileNamespace = new Zend_Session_Namespace('userProfileNamespace', true); // 限制命名空间访问单一实例,不要忘了true if (isset($userProfileNamespace->name)) { $username = $userProfileNamespace->name; } ?>(3)若在限定时间内访问该值,只能访问1次;若超过限定不访问该值,将会自动失效。