ZendFramework中使用session

[參看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次;若超過限定不訪問該值,將會自動失效。
相關文章
相關標籤/搜索