Zend_Session-BasicUsage.xml 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.session.basic_usage">
  4. <title>Basic Usage</title>
  5. <para>
  6. <classname>Zend_Session_Namespace</classname> instances provide the primary
  7. <acronym>API</acronym> for manipulating session data in the Zend Framework. Namespaces are
  8. used to segregate all session data, although a default namespace exists for those who only
  9. want one namespace for all their session data. <classname>Zend_Session</classname> utilizes
  10. ext/session and its special <varname>$_SESSION</varname> superglobal as the storage
  11. mechanism for session state data. while <varname>$_SESSION</varname> is still available in
  12. <acronym>PHP</acronym>'s global namespace, developers should refrain from directly accessing
  13. it, so that <classname>Zend_Session</classname> and
  14. <classname>Zend_Session_Namespace</classname> can most effectively and securely provide its
  15. suite of session related functionality.
  16. </para>
  17. <para>
  18. Each instance of <classname>Zend_Session_Namespace</classname> corresponds to an entry of
  19. the <varname>$_SESSION</varname> superglobal array, where the namespace is used as the key.
  20. <programlisting language="php"><![CDATA[
  21. $myNamespace = new Zend_Session_Namespace('myNamespace');
  22. // $myNamespace corresponds to $_SESSION['myNamespace']
  23. ]]></programlisting>
  24. It is possible to use Zend_Session in conjunction with other code that uses
  25. <varname>$_SESSION</varname> directly. To avoid problems, however, it is highly recommended
  26. that such code only uses parts of <varname>$_SESSION</varname> that do not correspond to
  27. instances of <classname>Zend_Session_Namespace</classname>.
  28. </para>
  29. <sect2 id="zend.session.basic_usage.basic_examples">
  30. <title>Tutorial Examples</title>
  31. <para>
  32. If no namespace is specified when instantiating
  33. <classname>Zend_Session_Namespace</classname>, all data will be transparently stored in
  34. a namespace called "<code>Default</code>". <classname>Zend_Session</classname> is not
  35. intended to work directly on the contents of session namespace containers. Instead, we
  36. use <classname>Zend_Session_Namespace</classname>. The example below demonstrates use of
  37. this default namespace, showing how to count the number of client requests during a
  38. session:
  39. </para>
  40. <example id="zend.session.basic_usage.basic_examples.example.counting_page_views">
  41. <title>Counting Page Views</title>
  42. <programlisting language="php"><![CDATA[
  43. $defaultNamespace = new Zend_Session_Namespace('Default');
  44. if (isset($defaultNamespace->numberOfPageRequests)) {
  45. // this will increment for each page load.
  46. $defaultNamespace->numberOfPageRequests++;
  47. } else {
  48. $defaultNamespace->numberOfPageRequests = 1; // first time
  49. }
  50. echo "Page requests this session: ",
  51. $defaultNamespace->numberOfPageRequests;
  52. ]]></programlisting>
  53. </example>
  54. <para>
  55. When multiple modules use instances of <classname>Zend_Session_Namespace</classname>
  56. having different namespaces, each module obtains data encapsulation for its session
  57. data. The <classname>Zend_Session_Namespace</classname> constructor can be passed an
  58. optional <varname>$namespace</varname> argument, which allows developers to partition
  59. session data into separate namespaces. Namespacing provides an effective and popular way
  60. to secure session state data against changes due to accidental naming collisions.
  61. </para>
  62. <para>
  63. Namespace names are restricted to character sequences represented as non-empty
  64. <acronym>PHP</acronym> strings that do not begin with an underscore ("<code>_</code>")
  65. character. Only core components included in Zend Framework should use namespace names
  66. starting with "<code>Zend</code>".
  67. </para>
  68. <example id="zend.session.basic_usage.basic_examples.example.namespaces.new">
  69. <title>New Way: Namespaces Avoid Collisions</title>
  70. <programlisting language="php"><![CDATA[
  71. // in the Zend_Auth component
  72. $authNamespace = new Zend_Session_Namespace('Zend_Auth');
  73. $authNamespace->user = "myusername";
  74. // in a web services component
  75. $webServiceNamespace = new Zend_Session_Namespace('Some_Web_Service');
  76. $webServiceNamespace->user = "mywebusername";
  77. ]]></programlisting>
  78. </example>
  79. <para>
  80. The example above achieves the same effect as the code below, except that the session
  81. objects above preserve encapsulation of session data within their respective namespaces.
  82. </para>
  83. <example id="zend.session.basic_usage.basic_examples.example.namespaces.old">
  84. <title>Old Way: PHP Session Access</title>
  85. <programlisting language="php"><![CDATA[
  86. $_SESSION['Zend_Auth']['user'] = "myusername";
  87. $_SESSION['Some_Web_Service']['user'] = "mywebusername";
  88. ]]></programlisting>
  89. </example>
  90. </sect2>
  91. <sect2 id="zend.session.basic_usage.iteration">
  92. <title>Iterating Over Session Namespaces</title>
  93. <para>
  94. <classname>Zend_Session_Namespace</classname> provides the full <ulink
  95. url="http://www.php.net/~helly/php/ext/spl/interfaceIteratorAggregate.html">IteratorAggregate
  96. interface</ulink>, including support for the <code>foreach</code> statement:
  97. </para>
  98. <example id="zend.session.basic_usage.iteration.example">
  99. <title>Session Iteration</title>
  100. <programlisting language="php"><![CDATA[
  101. $aNamespace =
  102. new Zend_Session_Namespace('some_namespace_with_data_present');
  103. foreach ($aNamespace as $index => $value) {
  104. echo "aNamespace->$index = '$value';\n";
  105. }
  106. ]]></programlisting>
  107. </example>
  108. </sect2>
  109. <sect2 id="zend.session.basic_usage.accessors">
  110. <title>Accessors for Session Namespaces</title>
  111. <para>
  112. <classname>Zend_Session_Namespace</classname> implements the
  113. <methodname>__get()</methodname>, <methodname>__set()</methodname>,
  114. <methodname>__isset()</methodname>, and <methodname>__unset()</methodname> <ulink
  115. url="http://www.php.net/manual/en/language.oop5.overloading.php">magic methods</ulink>,
  116. which should not be invoked directly, except from within a subclass. Instead, the normal
  117. operators automatically invoke these methods, such as in the following example:
  118. </para>
  119. <example id="zend.session.basic_usage.accessors.example">
  120. <title>Accessing Session Data</title>
  121. <programlisting language="php"><![CDATA[
  122. $namespace = new Zend_Session_Namespace(); // default namespace
  123. $namespace->foo = 100;
  124. echo "\$namespace->foo = $namespace->foo\n";
  125. if (!isset($namespace->bar)) {
  126. echo "\$namespace->bar not set\n";
  127. }
  128. unset($namespace->foo);
  129. ]]></programlisting>
  130. </example>
  131. </sect2>
  132. </sect1>