Zend_Session operation details in the Zend Framework getting started tutorial, zendzend_session
This article describes the Zend_Session operation in the Zend Framework getting started tutorial. We will share this with you for your reference. The details are as follows:
Session namespace
Implement session
Code:
<? Phprequire_once "Zend/Session/Namespace. php "; $ myNamespace = new Zend_Session_Namespace ('myspace '); if (isset ($ myNamespace-> numberOfPageRequests) {$ myNamespace-> numberOfPageRequests ++ ;} else {$ myNamespace-> numberOfPageRequests = 1;} echo "user views :"; echo "<font size = \" 6 \ "color = \" # ff0000 \ ">"; echo $ myNamespace-> numberOfPageRequests; echo "</font> times ";
Result:
User browsing times: 10
Traverse session namespace
Code:
<? Phprequire_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 "in namespace myNamespace :". $ index; echo "is ". $ value. "<p> \ n ";}
Result:
In the namespace myNamespace, The webhost is 127.0.0.1.
In the namespace myNamespace, the hostname is localhost.
In the namespace myNamespace, the user is root.
In the namespace myNamespace, the password is 123456.
In the namespace myNamespace, db_name is test.
In the namespace myNamespace, db_type is Sqlite.
Comments:
It traverses all the content in the corresponding space of this object. Amazing.
Access session namespace
Code:
<? Phprequire_once "Zend/Session/Namespace. php "; $ login = new Zend_Session_Namespace ('other'); $ login-> user =" Administrator "; if (isset ($ login-> user )) {echo "\ $ login-> user has a value. The value is:"; echo $ login-> user; unset ($ login-> user );} else {echo "\ $ login-> user no value";} echo "<p>"; if (isset ($ login-> pass )) {echo "\ $ login-> pass already has a value. Its value is:"; echo $ login-> pass; unset ($ login-> pass );} else {echo "\ $ login-> pass without value";} foreach ($ login as $ index => $ value) {echo "namespace login :". $ index. "is ". $ value. "<p> \ n ";}
Result:
$ Login-> user has a value, whose value is Administrator.
$ Login-> pass no value
Session advanced applications
You can enable a session in either of the following ways:
1. Use Zend_Session: start () to enable the session
Ii. new Zend_Session_Namespace ()
Lock session namespace
Code:
<? Phprequire_once "Zend/Session/Namespace. php "; $ test = new Zend_Session_Namespace ('test'); $ test-> name =" "; $ test-> sex =" male "; $ test-> lock (); if ($ test-> isLocked () {echo "session \ $ test is locked! <P> "; echo" The member name value in namespace \ $ test is: "; echo $ test-> name;} else {echo" session \ $ test has been unlocked! ";}Echo" <p> "; $ test-> unLock (); if ($ test-> isLocked () {echo" session \ $ test is locked! <P> "; echo" The member name value in namespace \ $ test is: "; echo $ test-> name;} else {echo" session \ $ test has been unlocked! ";}
Result:
Session $ test is locked!
The member name in namespace $ test is:
Session $ test has been unlocked!
Comments:
Therefore, locking does not affect the output of results.
Analyze source code
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]);}
We can see that it only changes the parameter.
Set lifecycle for sessions
SetExpirationSeconds () method and setExpirationHops () method.
Code:
<? Phprequire_once "Zend/Session/Namespace. php "; $ s = new Zend_Session_Namespace ('temp '); $ s-> a =" apple "; $ s-> p =" Pear "; $ s-> o = "orange"; $ s-> setExpirationSeconds (60); $ s-> setExpirationHops (2, 'A'); $ s-> setExpirationHops (3, 'P'); echo "Life Cycle set for namespace \ $ s <p> ";
The lifecycle code is actually set for namespaces.
Test code:
<? Phprequire_once "Zend/Session/Namespace. php "; $ B = new Zend_Session_Namespace ('temp '); echo" \ $ B-> a content :". $ B-> a; echo "<p>"; echo "\ $ B-> p content :". $ B-> p;
First, execute the set Life Cycle Code, and the test code will be executed to see the effect.
First time:
$ B-> a: Apple
$ B-> p content: Pear
Second:
$ B-> a: Apple
$ B-> p content: Pear
Third time:
$ B->:
$ B-> p content: Pear
Fourth:
$ B->:
$ B-> p content:
Comment: After refreshing twice, it will disappear. And then disappear. The effect is the same for more than 60 seconds.
Analyze source code,
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; } } }}
In fact, it is implemented based on the original PHP Session. Only some functions are extended.
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; } } } }}
Processing is placed in the constructor.