Zend Framework Introductory Tutorials _php Examples of zend_session session operations

Source: Internet
Author: User
Tags pear sqlite webhost zend zend framework

This example describes the zend_session session operation of the Zend Framework Introductory tutorial. Share to everyone for your reference, specific as follows:

Session namespace

Implementing a session

Code:

<?php
require_once "zend/session/namespace.php";
$myNamespace = new Zend_session_namespace (' Myspace ');
if (Isset ($myNamespace->numberofpagerequests))
{
  $myNamespace->numberofpagerequests++;
} else{
  $myNamespace->numberofpagerequests = 1;
}
echo "User's browsing times are:";
echo "<font size=\" 6\ "color=\" #ff0000 \ ">";
echo $myNamespace->numberofpagerequests;
echo "</font> times";

Results:

User browse times: 10 times

Traversing the session namespace

Code:

<?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 namespace MyNamespace: ". $index;
  echo "to". $value. " <p>\n ";
}

Results:

In the namespace MyNamespace: Webhost is 127.0.0.1
In the namespace MyNamespace: hostname is localhost
In namespace MyNamespace: User is root
In the namespace MyNamespace: password is 123456
In the namespace MyNamespace: Db_name is test
In the namespace MyNamespace: Db_type is SQLite

Comments:

It iterates through all the contents of the object's corresponding space. It's amazing.

Access Session Namespace

Code:

<?php
require_once "zend/session/namespace.php";
$login = new Zend_session_namespace (' other ');
$login->user = "Administrator";
if (Isset ($login->user)) {
  echo "\ $login->user already has a value of:";
  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 of:";
  echo $login->pass;
  unset ($login->pass);
else{
  echo "\ $login->pass no value";
}
foreach ($login as $index => $value) {
  echo Namespace login: ". $index." To ". $value." <p>\n ";
}

Results:

$login->user already has a value of: Administrator
$login->pass No value

Advanced Applications for Sessions

There are two ways to open a session

First, use Zend_session::start () to open the session

II. New Zend_session_namespace ()

Lock Session Namespace

Code:

<?php
require_once "zend/session/namespace.php";
$test = new Zend_session_namespace (' Test ');
$test->name = "Jade Emperor";
$test->sex = "male";
$test->lock ();
if ($test->islocked ()) {
  echo session \ $test is locked! <p> ";
  The value of member name in the Echo 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> ";
  The value of member name in the Echo namespace \ $test is: ";
  echo $test->name;
} else{
  echo "session \ $test has been unlocked! ";
}

Results:

Session $test already locked!

The value of member name in the namespace $test is: Jade Emperor

Session $test already unlocked!

Comments:

Thus, locking does not affect the output of the result.

Analyzing source code

Public Function Lock ()
{
    self::$_namespacelocks[$this->_namespace] = true;
}
/**
* Unlock ()-Unmark a session/namespace to enable read & write
*
* @return void */public
func tion 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, with only if, read-only
*
* @return bool
/
Public function islocked ()
{return
    isset (self::$_namespacelocks[$this->_namespace]);


It just changes the parameters.

Set the lifecycle for a session

The Setexpirationseconds () method is set with the Setexpirationhops () method.

Code:

<?php
require_once "zend/session/namespace.php";
$s = new Zend_session_namespace (' temp ');
$s->a = "Apple";
$s->p = "pear";
$s->o = "orange";
$s->setexpirationseconds (a);
$s->setexpirationhops (2, ' a ');
$s->setexpirationhops (3, ' P ');
Echo has set life <p> for namespaces \ $s;

Set the lifecycle code, in fact it is for the namespace to set.

Test code:

<?php
require_once "zend/session/namespace.php";
$b = new Zend_session_namespace (' temp ');
echo "\ $b->a content is:". $b->a;
echo "<p>";
echo "\ $b->p content is:". $b->p;

The set lifecycle code is executed first, and the effect is seen in the execution of the test code.

First time:
$b->a content is: Apple
$b->p content is: Pear
Second time:
$b->a content is: Apple
$b->p content is: Pear
Third time:
$b->a content is:
$b->p content is: Pear
Fourth time:
$b->a content is:
$b->p content is:

Comment: Refresh two times, there will be disappeared. Then disappeared. The effect is the same for more than 60 seconds.

Analyze source code,

 Public function setexpirationseconds ($seconds, $variables = null) {if (Parent::$_wri Table = = 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/ex
      Ception.php ';
    throw new Zend_session_exception (' Seconds must be positive. '); } if ($variables = = null) {//apply expiration to entire namespace $_session[' __zf ' [$this->_namespac
    e][' ENT ' = time () + $seconds;
      else {if (is_string ($variables)) {$variables = array ($variables); foreach ($variables as $variable) {if (!empty ($variable)) {$_session[' __zf '] [$this->_namesp
        ace][' ENVT ' [$variable] = time () + $seconds; }
      }
    }
}

It's actually based on PHP's original session. Only some of the features are expanded.

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/EXCEP
      Tion.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;

 }
        }
      }
    }
}

The processing is placed in the constructor.

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.