/** * __construct() - This will create an instance that saves to/gets from an * instantiated core. An optional namespace allows for saving/getting * to isolated sections of the session. * * @param string $namespace */ public function __construct($namespace = 'Default') { if (!is_string($namespace)) { throw new Zend_Session_Exception("Namespace must be a string."); } if ($namespace[0] == "_") { throw new Zend_Session_Exception("Namespace must not start with an underscore."); } $this->_namespace = $namespace; $this->_session_core = Zend_Session_Core::getInstance(); $this->_session_core->_startNamespace($namespace); }
public function _ZF_expireAll($args) { Zend_Session_Core::setOptions(array('remember_me_seconds' => 15, 'gc_probability' => 2)); session_id($args[0]); if (isset($args[1]) && !empty($args[1])) { $s = new Zend_Session($args[1]); } else { $s = new Zend_Session(); } $result = ''; foreach ($s->getIterator() as $key => $val) { $result .= "{$key} === {$val};"; } $core = Zend_Session_Core::getInstance(); $core->expireSessionCookie(); $core->writeClose(); echo $result; }
/** * __construct() - This will create an instance that saves to/gets from an * instantiated core. An optional namespace allows for saving/getting * to isolated sections of the session. An optional argument $singleInstance * will prevent any futured attempts of getting a Zend_Session object in the * same namespace that is provided. * * @param string $namespace - programmatic name of the requested namespace * @param bool $singleInstance - prevent creation of additional instances for this namespace * @param Zend_Session_Core $core - OPTIONAL instance of Zend_Session_Core, used only for testing purposes * @return void */ public function __construct($namespace = 'Default', $singleInstance = false, Zend_Session_Core $core = null) { if ($namespace === '') { throw Zend::exception('Zend_Session_Exception', 'Session namespace must be a non-empty string.'); } if ($namespace[0] == "_") { throw Zend::exception('Zend_Session_Exception', 'Session namespace must not start with an underscore.'); } if (isset(self::$_singleInstances[$namespace])) { throw Zend::exception('Zend_Session_Exception', 'A session namespace "' . $namespace . '" already exists and has been set as the only instance of this namespace.'); } if ($singleInstance === true) { self::$_singleInstances[$namespace] = true; } $this->_namespace = $namespace; $this->_sessionCore = $core ? $core : Zend_Session_Core::getInstance(); $this->_sessionCore->_startNamespace($namespace); }
/** * test expiration of namespace variables by hops * expect expiration of specified keys in the proper number of hops */ public function testSetExpireSessionVarsByHopsOnUse() { $s = new Zend_Session('expireGuava'); $expireBeforeHop = 2; $s->setExpirationHops($expireBeforeHop, 'g', true); // only count a hop, when namespace is used $s->g = 'guava'; $s->p = 'peach'; $s->p = 'plum'; $id = session_id(); session_write_close(); // release session so process below can use it // we are not accessing (using) the "expireGuava" namespace, so these hops should have no effect for ($i = 1; $i <= $expireBeforeHop + 2; $i++) { exec($this->script . "expireAll {$id} notused", $result); $result = $this->sortResult($result); $this->assertTrue($result === '', "iteration over named Zend_Session namespace failed (result='{$result}'; hop #{$i})"); } for ($i = 1; $i <= $expireBeforeHop + 2; $i++) { exec($this->script . "expireAll {$id} expireGuava", $result); $result = $this->sortResult($result); if ($i > $expireBeforeHop) { $expect = ';p === plum'; $this->assertTrue($result === $expect, "unexpected results iterating over named Zend_Session namespace (result='{$result}'; expected '{$expect}'; hop #{$i})"); } else { $expect = ';g === guava;p === plum'; $this->assertTrue($result === $expect, "unexpected results iterating over named Zend_Session namespace (result='{$result}'; expected '{$expect}'; hop #{$i})"); } } session_start(); // resume artificially suspended session $core = Zend_Session_Core::getInstance(); $core->destroy(); }
/** * test expiration of namespaces and namespace variables by seconds * expect expiration of specified keys/namespace */ public function testSetExpirationSeconds() { $s = new Zend_Session('expireAll'); $s->a = 'apple'; $s->p = 'pear'; $s->o = 'orange'; $s->setExpirationSeconds(5); $core = Zend_Session_Core::getInstance(); $core->regenerateId(); $id = Zend_Session_Core::getId(); session_write_close(); // release session so process below can use it sleep(4); // not long enough for things to expire exec($this->script . "expireAll {$id} expireAll", $result); $result = $this->sortResult($result); $expect = ';a === apple;o === orange;p === pear'; $this->assertTrue($result === $expect, "iteration over default Zend_Session namespace failed; expecting result === '{$expect}', but got '{$result}'"); sleep(2); // long enough for things to expire (total of 6 seconds waiting, but expires in 5) exec($this->script . "expireAll {$id} expireAll", $result); $result = array_pop($result); $this->assertTrue($result === '', "iteration over default Zend_Session namespace failed; expecting result === '', but got '{$result}')"); session_start(); // resume artificially suspended session // We could split this into a separate test, but actually, if anything leftover from above // contaminates the tests below, that is also a bug that we want to know about. $s = new Zend_Session('expireGuava'); $s->setExpirationSeconds(5, 'g'); // now try to expire only 1 of the keys in the namespace $s->g = 'guava'; $s->p = 'peach'; $s->p = 'plum'; session_write_close(); // release session so process below can use it sleep(6); // not long enough for things to expire exec($this->script . "expireAll {$id} expireGuava", $result); $result = $this->sortResult($result); session_start(); // resume artificially suspended session $this->assertTrue($result === ';p === plum', "iteration over named Zend_Session namespace failed (result={$result})"); }