Zend_Session-BasicUsage.xml 6.9 KB

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