Home > php教程 > PHP开发 > body text

Detailed explanation of Zend_Session session operation in Zend Framework introductory tutorial

高洛峰
Release: 2017-01-06 10:02:37
Original
1863 people have browsed it

本文实例讲述了Zend Framework入门教程之Zend_Session会话操作。分享给大家供大家参考,具体如下:

会话命名空间

实现会话

代码:

<?php
require_once "Zend/Session/Namespace.php";
$myNamespace = new Zend_Session_Namespace(&#39;Myspace&#39;);
if(isset($myNamespace->numberOfPageRequests))
{
  $myNamespace->numberOfPageRequests++;
}else{
  $myNamespace->numberOfPageRequests = 1;
}
echo "用户的浏览次数为:";
echo "<font size=\"6\" color=\"#ff0000\">";
echo $myNamespace->numberOfPageRequests;
echo "</font>次";
Copy after login

结果:

用户的浏览次数为:10次

遍历会话命名空间

代码:

<?php
require_once "Zend/Session/Namespace.php";
$myNamespace = new Zend_Session_Namespace(&#39;Myspace&#39;);
$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 "命名空间myNamespace中的:".$index;
  echo "为".$value."<p>\n";
}
Copy after login

结果:

命名空间myNamespace中的:webhost为127.0.0.1
命名空间myNamespace中的:hostname为localhost
命名空间myNamespace中的:user为root
命名空间myNamespace中的:password为123456
命名空间myNamespace中的:db_name为test
命名空间myNamespace中的:db_type为Sqlite

点评:

它会把这个对象所对应空间中的所有内容遍历出来。很神奇。

访问会话命名空间

代码:

<?php
require_once "Zend/Session/Namespace.php";
$login = new Zend_Session_Namespace(&#39;other&#39;);
$login->user = "Administrator";
if(isset($login->user)){
  echo "\$login->user已经有值,其值为:";
  echo $login->user;
  unset($login->user);
}else{
  echo "\$login->user无值";
}
echo "<p>";
if(isset($login->pass)){
  echo "\$login->pass已经有值,其值为:";
  echo $login->pass;
  unset($login->pass);
}else{
  echo "\$login->pass无值";
}
foreach($login as $index=>$value){
  echo "命名空间login中的:".$index."为".$value."<p>\n";
}
Copy after login

结果:

$login->user已经有值,其值为:Administrator
$login->pass无值

会话的高级应用

开启会话,有两种方法

一、使用Zend_Session::start()开启会话

二、new Zend_Session_Namespace()

锁定会话命名空间

代码:

<?php
require_once "Zend/Session/Namespace.php";
$test = new Zend_Session_Namespace(&#39;test&#39;);
$test->name = "玉皇大帝";
$test->sex = "男";
$test->lock();
if($test->isLocked()){
  echo "会话\$test已经锁定!<p>";
  echo "命名空间\$test中的成员name的值为:";
  echo $test->name;
}else{
  echo "会话\$test已经解锁!";
}
echo "<p>";
$test->unLock();
if($test->isLocked()){
  echo "会话\$test已经锁定!<p>";
  echo "命名空间\$test中的成员name的值为:";
  echo $test->name;
}else{
  echo "会话\$test已经解锁!";
}
Copy after login

结果:

会话$test已经锁定!

命名空间$test中的成员name的值为:玉皇大帝

会话$test已经解锁!

点评:

由此可见,锁定并不影响结果的输出。

分析源代码

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]);
}
Copy after login

可知,它只是改变了参数而已。

为会话设置生命周期

setExpirationSeconds()方法与setExpirationHops()两种方法来设置。

代码:

<?php
require_once "Zend/Session/Namespace.php";
$s = new Zend_Session_Namespace(&#39;temp&#39;);
$s->a = "苹果";
$s->p = "梨";
$s->o = "桔子";
$s->setExpirationSeconds(60);
$s->setExpirationHops(2,&#39;a&#39;);
$s->setExpirationHops(3,&#39;p&#39;);
echo "已经为命名空间\$s设置生命期<p>";
Copy after login

设置生命期代码,其实它针对的是命名空间来设置的。

测试代码:

<?php
require_once "Zend/Session/Namespace.php";
$b = new Zend_Session_Namespace(&#39;temp&#39;);
echo "\$b->a内容为:".$b->a;
echo "<p>";
echo "\$b->p内容为:".$b->p;
Copy after login

先执行设置生命期代码,在执行测试代码会看到效果。

第一次:
$b->a内容为:苹果
$b->p内容为:梨
第二次:
$b->a内容为:苹果
$b->p内容为:梨
第三次:
$b->a内容为:
$b->p内容为:梨
第四次:
$b->a内容为:
$b->p内容为:

点评:刷新两次之后,就会有消失。之后陆续消失。超过60秒效果相同。

分析源代码,

public function setExpirationSeconds($seconds, $variables = null)
{
    if (parent::$_writable === false) {
      /**
       * @see Zend_Session_Exception
       */
      require_once &#39;Zend/Session/Exception.php&#39;;
      throw new Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG);
    }
    if ($seconds <= 0) {
      /**
       * @see Zend_Session_Exception
       */
      require_once &#39;Zend/Session/Exception.php&#39;;
      throw new Zend_Session_Exception(&#39;Seconds must be positive.&#39;);
    }
    if ($variables === null) {
      // apply expiration to entire namespace
      $_SESSION[&#39;__ZF&#39;][$this->_namespace][&#39;ENT&#39;] = time() + $seconds;
    } else {
      if (is_string($variables)) {
        $variables = array($variables);
      }
      foreach ($variables as $variable) {
        if (!empty($variable)) {
          $_SESSION[&#39;__ZF&#39;][$this->_namespace][&#39;ENVT&#39;][$variable] = time() + $seconds;
        }
      }
    }
}
Copy after login

其实它还是基于PHP原始的Session来实现的。只是扩展了部分功能。

public function setExpirationHops($hops, $variables = null, $hopCountOnUsageOnly = false)
{
    if (parent::$_writable === false) {
      /**
       * @see Zend_Session_Exception
       */
      require_once &#39;Zend/Session/Exception.php&#39;;
      throw new Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG);
    }
    if ($hops <= 0) {
      /**
       * @see Zend_Session_Exception
       */
      require_once &#39;Zend/Session/Exception.php&#39;;
      throw new Zend_Session_Exception(&#39;Hops must be positive number.&#39;);
    }
    if ($variables === null) {
      // apply expiration to entire namespace
      if ($hopCountOnUsageOnly === false) {
        $_SESSION[&#39;__ZF&#39;][$this->_namespace][&#39;ENGH&#39;] = $hops;
      } else {
        $_SESSION[&#39;__ZF&#39;][$this->_namespace][&#39;ENNH&#39;] = $hops;
      }
    } else {
      if (is_string($variables)) {
        $variables = array($variables);
      }
      foreach ($variables as $variable) {
        if (!empty($variable)) {
          if ($hopCountOnUsageOnly === false) {
            $_SESSION[&#39;__ZF&#39;][$this->_namespace][&#39;ENVGH&#39;][$variable] = $hops;
          } else {
            $_SESSION[&#39;__ZF&#39;][$this->_namespace][&#39;ENVNH&#39;][$variable] = $hops;
          }
        }
      }
    }
}
Copy after login

处理放在了构造函数中。

希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。

更多Zend Framework入门教程之Zend_Session会话操作详解相关文章请关注PHP中文网!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template