In this example, we create and destroy sessions with namespaces.
Components used in this example
Implementation of a session
class MySession
{
Processing the session- We get the namespace, the action, the write lock, the expiration, the number of hops, and the option to regenerate the session.
- If the user requests the page without a session, we simply display the page.
- Otherwise, we start a session.
- We regenerate the session identifier if requested.
- Then we start or we stop or we simply use the session with a given namespace.
- If we catch an exception, we return the error message.
public function process()
{
// We get the namespace, the action, the write lock, the expiration,
// the number of hops, and the option to regenerate the session.
list($namespace, $action, $lock, $expire, $hops, $regenerate) =
$this->_getParameters();
try {
if ($namespace == 'none') {
// If the user requests the page without a session,
// we simply display the page.
$result = 'You requested this page without a session.';
} else {
// Otherwise, we start a session.
Zend_Session::start();
// We regenerate the session identifier if requested.
$regenerate and Zend_Session::regenerateId();
// Then we start or we stop or we simply use the session with a given namespace.
switch($action) {
case 'Start':
$result = $this->_start($namespace, $expire, $hops);
break;
case 'Stop':
$result = $this->_stop($namespace);
break;
case 'Use':
default:
$result = $this->_use($namespace, $expire, $hops, $lock);
break;
}
}
} catch (Exception $e) {
// If we catch an exception, we return the error message.
$result = $e->getMessage();
}
return array($namespace, $result);
}
Extraction of the parameters from the GET request
private function _getParameters()
{
$namespace = isset($_GET['namespace'])? $_GET['namespace'] : 'Default';
$action = isset($_GET['action'])? $_GET['action'] : null;
$lock = !empty($_GET['lock']);
$expire = isset($_GET['expire'])? $_GET['expire'] : null;
$hops = isset($_GET['hops'])? $_GET['hops'] : null;
$regenerate = !empty($_GET['regenerate']);
return array($namespace, $action, $lock, $expire, $hops, $regenerate);
}
Starting a namespace session- We initialize the namespace if needed.
- And we update the namespace.
- We return the data for that namespace.
private function _start($namespace, $expire, $hops)
{
$session = new MyNamespace($namespace);
// We initialize the namespace if needed.
Zend_Session::namespaceIsset($namespace) or $session->create();
// And we update the namespace.
$session->update($expire, $hops);
// We return the data for that namespace.
return $session->get();
}
Stopping a namespace session- We remove the namespace if the namespace exists.
private function _stop($namespace)
{
$session = new MyNamespace($namespace);
if (Zend_Session::namespaceIsset($namespace)) {
// We remove the namespace if the namespace exists.
$session->remove();
$result = "Session \"$namespace\" destroyed!";
} else {
$result = "There is no such session: \"$namespace\"!";
}
return $result;
}
Using a namespace session- We lock the name space if requested.
- We update the namespace.
- We return the data for that namespace.
private function _use($namespace, $expire, $hops, $lock)
{
if (Zend_Session::namespaceIsset($namespace)) {
$session = new MyNamespace($namespace);
// We lock the name space if requested.
$lock and $session->lock();
// We update the namespace.
$session->update($expire, $hops);
// We return the data for that namespace.
$result = $session->get();
} else {
$result = "Please start the session: \"$namespace\"!";
}
return $result;
}
}
No comments:
Post a Comment