欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Zend Framework入門(mén)教程之Zend_Session會(huì)話操作詳解

 更新時(shí)間:2016年12月08日 11:34:08   作者:牛逼的霍嘯林  
這篇文章主要介紹了Zend Framework入門(mén)教程之Zend_Session會(huì)話操作,結(jié)合實(shí)例形式詳細(xì)分析了Zend_Session會(huì)話操作的具體使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Zend Framework入門(mén)教程之Zend_Session會(huì)話操作。分享給大家供大家參考,具體如下:

會(huì)話命名空間

實(shí)現(xiàn)會(huì)話

代碼:

<?php
require_once "Zend/Session/Namespace.php";
$myNamespace = new Zend_Session_Namespace('Myspace');
if(isset($myNamespace->numberOfPageRequests))
{
  $myNamespace->numberOfPageRequests++;
}else{
  $myNamespace->numberOfPageRequests = 1;
}
echo "用戶的瀏覽次數(shù)為:";
echo "<font size=\"6\" color=\"#ff0000\">";
echo $myNamespace->numberOfPageRequests;
echo "</font>次";

結(jié)果:

用戶的瀏覽次數(shù)為:10次

遍歷會(huì)話命名空間

代碼:

<?php
require_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 "命名空間myNamespace中的:".$index;
  echo "為".$value."<p>\n";
}

結(jié)果:

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

點(diǎn)評(píng):

它會(huì)把這個(gè)對(duì)象所對(duì)應(yīng)空間中的所有內(nèi)容遍歷出來(lái)。很神奇。

訪問(wèn)會(huì)話命名空間

代碼:

<?php
require_once "Zend/Session/Namespace.php";
$login = new Zend_Session_Namespace('other');
$login->user = "Administrator";
if(isset($login->user)){
  echo "\$login->user已經(jīng)有值,其值為:";
  echo $login->user;
  unset($login->user);
}else{
  echo "\$login->user無(wú)值";
}
echo "<p>";
if(isset($login->pass)){
  echo "\$login->pass已經(jīng)有值,其值為:";
  echo $login->pass;
  unset($login->pass);
}else{
  echo "\$login->pass無(wú)值";
}
foreach($login as $index=>$value){
  echo "命名空間login中的:".$index."為".$value."<p>\n";
}

結(jié)果:

$login->user已經(jīng)有值,其值為:Administrator
$login->pass無(wú)值

會(huì)話的高級(jí)應(yīng)用

開(kāi)啟會(huì)話,有兩種方法

一、使用Zend_Session::start()開(kāi)啟會(huì)話

二、new Zend_Session_Namespace()

鎖定會(huì)話命名空間

代碼:

<?php
require_once "Zend/Session/Namespace.php";
$test = new Zend_Session_Namespace('test');
$test->name = "玉皇大帝";
$test->sex = "男";
$test->lock();
if($test->isLocked()){
  echo "會(huì)話\$test已經(jīng)鎖定!<p>";
  echo "命名空間\$test中的成員name的值為:";
  echo $test->name;
}else{
  echo "會(huì)話\$test已經(jīng)解鎖!";
}
echo "<p>";
$test->unLock();
if($test->isLocked()){
  echo "會(huì)話\$test已經(jīng)鎖定!<p>";
  echo "命名空間\$test中的成員name的值為:";
  echo $test->name;
}else{
  echo "會(huì)話\$test已經(jīng)解鎖!";
}

結(jié)果:

會(huì)話$test已經(jīng)鎖定!

命名空間$test中的成員name的值為:玉皇大帝

會(huì)話$test已經(jīng)解鎖!

點(diǎn)評(píng):

由此可見(jiàn),鎖定并不影響結(jié)果的輸出。

分析源代碼

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

可知,它只是改變了參數(shù)而已。

為會(huì)話設(shè)置生命周期

setExpirationSeconds()方法與setExpirationHops()兩種方法來(lái)設(shè)置。

代碼:

<?php
require_once "Zend/Session/Namespace.php";
$s = new Zend_Session_Namespace('temp');
$s->a = "蘋(píng)果";
$s->p = "梨";
$s->o = "桔子";
$s->setExpirationSeconds(60);
$s->setExpirationHops(2,'a');
$s->setExpirationHops(3,'p');
echo "已經(jīng)為命名空間\$s設(shè)置生命期<p>";

設(shè)置生命期代碼,其實(shí)它針對(duì)的是命名空間來(lái)設(shè)置的。

測(cè)試代碼:

<?php
require_once "Zend/Session/Namespace.php";
$b = new Zend_Session_Namespace('temp');
echo "\$b->a內(nèi)容為:".$b->a;
echo "<p>";
echo "\$b->p內(nèi)容為:".$b->p;

先執(zhí)行設(shè)置生命期代碼,在執(zhí)行測(cè)試代碼會(huì)看到效果。

第一次:
$b->a內(nèi)容為:蘋(píng)果
$b->p內(nèi)容為:梨
第二次:
$b->a內(nèi)容為:蘋(píng)果
$b->p內(nèi)容為:梨
第三次:
$b->a內(nèi)容為:
$b->p內(nèi)容為:梨
第四次:
$b->a內(nèi)容為:
$b->p內(nèi)容為:

點(diǎn)評(píng):刷新兩次之后,就會(huì)有消失。之后陸續(xù)消失。超過(guò)60秒效果相同。

分析源代碼,

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;
        }
      }
    }
}

其實(shí)它還是基于PHP原始的Session來(lái)實(shí)現(xiàn)的。只是擴(kuò)展了部分功能。

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;
          }
        }
      }
    }
}

處理放在了構(gòu)造函數(shù)中。

更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Zend FrameWork框架入門(mén)教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《Yii框架入門(mén)及常用技巧總結(jié)》、《ThinkPHP入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家基于Zend Framework框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論