Zend_Session-AdvancedUsage.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.session.advanced_usage">
  4. <title>Advanced Usage</title>
  5. <para>
  6. While the basic usage examples are a perfectly acceptable way to utilize Zend Framework sessions, there are some
  7. best practices to consider. This section discusses the finer details of session handling and illustrates more
  8. advanced usage of the <classname>Zend_Session</classname> component.
  9. </para>
  10. <sect2 id="zend.session.advanced_usage.starting_a_session">
  11. <title>Starting a Session</title>
  12. <para>
  13. If you want all requests to have a session facilitated by <classname>Zend_Session</classname>, then start the session in the
  14. bootstrap file:
  15. </para>
  16. <example id="zend.session.advanced_usage.starting_a_session.example">
  17. <title>Starting the Global Session</title>
  18. <programlisting language="php"><![CDATA[
  19. Zend_Session::start();
  20. ]]></programlisting>
  21. </example>
  22. <para>
  23. By starting the session in the bootstrap file, you avoid the possibility that your session might be started
  24. after headers have been sent to the browser, which results in an exception, and possibly a broken page for
  25. website viewers. Various advanced features require <methodname>Zend_Session::start()</methodname> first. (More on
  26. advanced features later.)
  27. </para>
  28. <para>
  29. There are four ways to start a session, when using <classname>Zend_Session</classname>. Two are wrong.
  30. </para>
  31. <orderedlist>
  32. <listitem>
  33. <para>
  34. Wrong: Do not enable PHP's <ulink
  35. url="http://www.php.net/manual/en/ref.session.php#ini.session.auto-start">
  36. <code>session.auto_start</code> setting</ulink>. If you do not have the
  37. ability to disable this setting in php.ini, you are using mod_php (or
  38. equivalent), and the setting is already enabled in <code>php.ini</code>, then
  39. add the following to your <code>.htaccess</code> file (usually in your HTML
  40. document root directory):
  41. <programlisting language="httpd.conf"><![CDATA[
  42. php_value session.auto_start 0
  43. ]]></programlisting>
  44. </para>
  45. </listitem>
  46. <listitem>
  47. <para>
  48. Wrong: Do not use <acronym>PHP</acronym>'s
  49. <ulink url="http://www.php.net/session_start"><methodname>session_start()</methodname></ulink> function
  50. directly. If you use <methodname>session_start()</methodname> directly, and then start using
  51. <classname>Zend_Session_Namespace</classname>, an exception will be thrown by
  52. <methodname>Zend_Session::start()</methodname> ("session has already been started"). If you call
  53. <methodname>session_start()</methodname> after using <classname>Zend_Session_Namespace</classname> or calling
  54. <methodname>Zend_Session::start()</methodname>, an error of level <constant>E_NOTICE</constant> will be generated, and
  55. the call will be ignored.
  56. </para>
  57. </listitem>
  58. <listitem>
  59. <para>
  60. Correct: Use <methodname>Zend_Session::start()</methodname>. If you want all requests to have and use
  61. sessions, then place this function call early and unconditionally in your bootstrap code.
  62. Sessions have some overhead. If some requests need sessions, but other requests will not need to use
  63. sessions, then:
  64. </para>
  65. <itemizedlist mark="opencircle">
  66. <listitem>
  67. <para>
  68. Unconditionally set the <code>strict</code> option to <constant>TRUE</constant> using
  69. <methodname>Zend_Session::setOptions()</methodname> in your bootstrap.
  70. </para>
  71. </listitem>
  72. <listitem>
  73. <para>
  74. Call <methodname>Zend_Session::start()</methodname> only for requests that need to use sessions and
  75. before any <classname>Zend_Session_Namespace</classname> objects are instantiated.
  76. </para>
  77. </listitem>
  78. <listitem>
  79. <para>
  80. Use "<code>new Zend_Session_Namespace()</code>" normally, where needed, but make sure
  81. <methodname>Zend_Session::start()</methodname> has been called previously.
  82. </para>
  83. </listitem>
  84. </itemizedlist>
  85. <para>
  86. The <code>strict</code> option prevents <code>new Zend_Session_Namespace()</code> from automatically
  87. starting the session using <methodname>Zend_Session::start()</methodname>. Thus, this option helps application
  88. developers enforce a design decision to avoid using sessions for certain requests, since it causes
  89. an exception to be thrown when <classname>Zend_Session_Namespace</classname> is instantiated before
  90. <methodname>Zend_Session::start()</methodname> is called. Developers should carefully consider the impact of
  91. using <methodname>Zend_Session::setOptions()</methodname>, since these options have global effect, owing to
  92. their correspondence to the underlying options for ext/session.
  93. </para>
  94. </listitem>
  95. <listitem>
  96. <para>
  97. Correct: Just instantiate <classname>Zend_Session_Namespace</classname> whenever needed, and the underlying
  98. <acronym>PHP</acronym> session will be automatically started. This offers extremely simple usage that works well in
  99. most situations. However, you then become responsible for ensuring that the first
  100. <code>new Zend_Session_Namespace()</code> happens <emphasis>before</emphasis> any
  101. output (e.g., <ulink url="http://www.php.net/headers_sent">HTTP headers</ulink>) has been sent by
  102. <acronym>PHP</acronym> to the client, if you are using the default, cookie-based sessions (strongly recommended). See
  103. <xref linkend="zend.session.global_session_management.headers_sent" /> for more information.
  104. </para>
  105. </listitem>
  106. </orderedlist>
  107. </sect2>
  108. <sect2 id="zend.session.advanced_usage.locking">
  109. <title>Locking Session Namespaces</title>
  110. <para>
  111. Session namespaces can be locked, to prevent further alterations to the data in that namespace. Use
  112. <methodname>lock()</methodname> to make a specific namespace read-only, <methodname>unLock()</methodname> to make a read-only
  113. namespace read-write, and <methodname>isLocked()</methodname> to test if a namespace has been previously locked. Locks
  114. are transient and do not persist from one request to the next. Locking the namespace has no effect on setter
  115. methods of objects stored in the namespace, but does prevent the use of the namespace's setter method to
  116. remove or replace objects stored directly in the namespace. Similarly, locking
  117. <classname>Zend_Session_Namespace</classname> instances does not prevent the use of symbol table aliases to the same
  118. data (see <ulink url="http://www.php.net/references">PHP references</ulink>).
  119. </para>
  120. <example id="zend.session.advanced_usage.locking.example.basic">
  121. <title>Locking Session Namespaces</title>
  122. <programlisting language="php"><![CDATA[
  123. $userProfileNamespace = new Zend_Session_Namespace('userProfileNamespace');
  124. // marking session as read only locked
  125. $userProfileNamespace->lock();
  126. // unlocking read-only lock
  127. if ($userProfileNamespace->isLocked()) {
  128. $userProfileNamespace->unLock();
  129. }
  130. ]]></programlisting>
  131. </example>
  132. </sect2>
  133. <sect2 id="zend.session.advanced_usage.expiration">
  134. <title>Namespace Expiration</title>
  135. <para>
  136. Limits can be placed on the longevity of both namespaces and individual keys in namespaces. Common use cases
  137. include passing temporary information between requests, and reducing exposure to certain security risks by
  138. removing access to potentially sensitive information some time after authentication occurred. Expiration can
  139. be based on either elapsed seconds or the number of "hops", where a hop occurs for each successive request.
  140. </para>
  141. <example id="zend.session.advanced_usage.expiration.example">
  142. <title>Expiration Examples</title>
  143. <programlisting language="php"><![CDATA[
  144. $s = new Zend_Session_Namespace('expireAll');
  145. $s->a = 'apple';
  146. $s->p = 'pear';
  147. $s->o = 'orange';
  148. $s->setExpirationSeconds(5, 'a'); // expire only the key "a" in 5 seconds
  149. // expire entire namespace in 5 "hops"
  150. $s->setExpirationHops(5);
  151. $s->setExpirationSeconds(60);
  152. // The "expireAll" namespace will be marked "expired" on
  153. // the first request received after 60 seconds have elapsed,
  154. // or in 5 hops, whichever happens first.
  155. ]]></programlisting>
  156. </example>
  157. <para>
  158. When working with data expiring from the session in the current request, care should be used when retrieving
  159. them. Although the data are returned by reference, modifying the data will not make expiring data persist
  160. past the current request. In order to "reset" the expiration time, fetch the data into temporary variables,
  161. use the namespace to unset them, and then set the appropriate keys again.
  162. </para>
  163. </sect2>
  164. <sect2 id="zend.session.advanced_usage.controllers">
  165. <title>Session Encapsulation and Controllers</title>
  166. <para>
  167. Namespaces can also be used to separate session access by controllers to protect variables from
  168. contamination. For example, an authentication controller might keep its session state data separate from all
  169. other controllers for meeting security requirements.
  170. </para>
  171. <example id="zend.session.advanced_usage.controllers.example">
  172. <title>Namespaced Sessions for Controllers with Automatic Expiration</title>
  173. <para>
  174. The following code, as part of a controller that displays a test question, initiates a boolean variable
  175. to represent whether or not a submitted answer to the test question should be accepted. In this case,
  176. the application user is given 300 seconds to answer the displayed question.
  177. </para>
  178. <programlisting language="php"><![CDATA[
  179. // ...
  180. // in the question view controller
  181. $testSpace = new Zend_Session_Namespace('testSpace');
  182. // expire only this variable
  183. $testSpace->setExpirationSeconds(300, 'accept_answer');
  184. $testSpace->accept_answer = true;
  185. //...
  186. ]]></programlisting>
  187. <para>
  188. Below, the controller that processes the answers to test questions determines whether or not to accept
  189. an answer based on whether the user submitted the answer within the allotted time:
  190. </para>
  191. <programlisting language="php"><![CDATA[
  192. // ...
  193. // in the answer processing controller
  194. $testSpace = new Zend_Session_Namespace('testSpace');
  195. if ($testSpace->accept_answer === true) {
  196. // within time
  197. }
  198. else {
  199. // not within time
  200. }
  201. // ...
  202. ]]></programlisting>
  203. </example>
  204. </sect2>
  205. <sect2 id="zend.session.advanced_usage.single_instance">
  206. <title>Preventing Multiple Instances per Namespace</title>
  207. <para>
  208. Although <link linkend="zend.session.advanced_usage.locking">session locking</link> provides a good degree
  209. of protection against unintended use of namespaced session data, <classname>Zend_Session_Namespace</classname> also
  210. features the ability to prevent the creation of multiple instances corresponding to a single namespace.
  211. </para>
  212. <para>
  213. To enable this behavior, pass <constant>TRUE</constant> to the second constructor argument when creating the last
  214. allowed instance of <classname>Zend_Session_Namespace</classname>. Any subsequent attempt to instantiate the same
  215. namespace would result in a thrown exception.
  216. </para>
  217. <example id="zend.session.advanced_usage.single_instance.example">
  218. <title>Limiting Session Namespace Access to a Single Instance</title>
  219. <programlisting language="php"><![CDATA[
  220. // create an instance of a namespace
  221. $authSpaceAccessor1 = new Zend_Session_Namespace('Zend_Auth');
  222. // create another instance of the same namespace, but disallow any
  223. // new instances
  224. $authSpaceAccessor2 = new Zend_Session_Namespace('Zend_Auth', true);
  225. // making a reference is still possible
  226. $authSpaceAccessor3 = $authSpaceAccessor2;
  227. $authSpaceAccessor1->foo = 'bar';
  228. assert($authSpaceAccessor2->foo, 'bar');
  229. try {
  230. $aNamespaceObject = new Zend_Session_Namespace('Zend_Auth');
  231. } catch (Zend_Session_Exception $e) {
  232. echo 'Cannot instantiate this namespace since ' .
  233. '$authSpaceAccessor2 was created\n';
  234. }
  235. ]]></programlisting>
  236. </example>
  237. <para>
  238. The second parameter in the constructor above tells <classname>Zend_Session_Namespace</classname> that any future
  239. instances with the "<classname>Zend_Auth</classname>" namespace are not allowed. Attempting to create such an instance
  240. causes an exception to be thrown by the constructor. The developer therefore becomes responsible for storing
  241. a reference to an instance object (<varname>$authSpaceAccessor1</varname>, <varname>$authSpaceAccessor2</varname>, or
  242. <varname>$authSpaceAccessor3</varname> in the example above) somewhere, if access to the session namespace is
  243. needed at a later time during the same request. For example, a developer may store the reference in a static
  244. variable, add the reference to a
  245. <ulink url="http://www.martinfowler.com/eaaCatalog/registry.html">registry</ulink> (see
  246. <xref linkend="zend.registry" />), or otherwise make it available to other methods that may need access to
  247. the session namespace.
  248. </para>
  249. </sect2>
  250. <sect2 id="zend.session.advanced_usage.arrays">
  251. <title>Working with Arrays</title>
  252. <para>
  253. Due to the implementation history of <acronym>PHP</acronym> magic methods, modifying an array inside a namespace may not work
  254. under <acronym>PHP</acronym> versions before 5.2.1. If you will only be working with <acronym>PHP</acronym> 5.2.1 or later, then you may <link
  255. linkend="zend.session.advanced_usage.objects">skip to the next section</link>.
  256. </para>
  257. <example id="zend.session.advanced_usage.arrays.example.modifying">
  258. <title>Modifying Array Data with a Session Namespace</title>
  259. <para>
  260. The following illustrates how the problem may be reproduced:
  261. </para>
  262. <programlisting language="php"><![CDATA[
  263. $sessionNamespace = new Zend_Session_Namespace();
  264. $sessionNamespace->array = array();
  265. // may not work as expected before PHP 5.2.1
  266. $sessionNamespace->array['testKey'] = 1;
  267. echo $sessionNamespace->array['testKey'];
  268. ]]></programlisting>
  269. </example>
  270. <example id="zend.session.advanced_usage.arrays.example.building_prior">
  271. <title>Building Arrays Prior to Session Storage</title>
  272. <para>
  273. If possible, avoid the problem altogether by storing arrays into a session namespace only after all
  274. desired array values have been set.
  275. </para>
  276. <programlisting language="php"><![CDATA[
  277. $sessionNamespace = new Zend_Session_Namespace('Foo');
  278. $sessionNamespace->array = array('a', 'b', 'c');
  279. ]]></programlisting>
  280. </example>
  281. <para>
  282. If you are using an affected version of <acronym>PHP</acronym> and need to modify the array after assigning it to a session
  283. namespace key, you may use either or both of the following workarounds.
  284. </para>
  285. <example id="zend.session.advanced_usage.arrays.example.workaround.reassign">
  286. <title>Workaround: Reassign a Modified Array</title>
  287. <para>
  288. In the code that follows, a copy of the stored array is created, modified, and reassigned to the
  289. location from which the copy was created, overwriting the original array.
  290. </para>
  291. <programlisting language="php"><![CDATA[
  292. $sessionNamespace = new Zend_Session_Namespace();
  293. // assign the initial array
  294. $sessionNamespace->array = array('tree' => 'apple');
  295. // make a copy of the array
  296. $tmp = $sessionNamespace->array;
  297. // modfiy the array copy
  298. $tmp['fruit'] = 'peach';
  299. // assign a copy of the array back to the session namespace
  300. $sessionNamespace->array = $tmp;
  301. echo $sessionNamespace->array['fruit']; // prints "peach"
  302. ]]></programlisting>
  303. </example>
  304. <example id="zend.session.advanced_usage.arrays.example.workaround.reference">
  305. <title>Workaround: store array containing reference</title>
  306. <para>
  307. Alternatively, store an array containing a reference to the desired array, and then access it
  308. indirectly.
  309. </para>
  310. <programlisting language="php"><![CDATA[
  311. $myNamespace = new Zend_Session_Namespace('myNamespace');
  312. $a = array(1, 2, 3);
  313. $myNamespace->someArray = array( &$a );
  314. $a['foo'] = 'bar';
  315. echo $myNamespace->someArray['foo']; // prints "bar"
  316. ]]></programlisting>
  317. </example>
  318. </sect2>
  319. <sect2 id="zend.session.advanced_usage.objects">
  320. <title>Using Sessions with Objects</title>
  321. <para>
  322. If you plan to persist objects in the <acronym>PHP</acronym> session, know that they will be
  323. <ulink url="http://www.php.net/manual/en/language.oop.serialization.php">serialized</ulink> for storage.
  324. Thus, any object persisted with the <acronym>PHP</acronym> session must be unserialized upon retrieval from storage. The
  325. implication is that the developer must ensure that the classes for the persisted objects must have been
  326. defined before the object is unserialized from session storage. If an unserialized object's class is not
  327. defined, then it becomes an instance of <code>stdClass</code>.
  328. </para>
  329. </sect2>
  330. <sect2 id="zend.session.advanced_usage.testing">
  331. <title>Using Sessions with Unit Tests</title>
  332. <para>
  333. Zend Framework relies on PHPUnit to facilitate testing of itself. Many developers extend the existing
  334. suite of unit tests to cover the code in their applications. The exception
  335. "<emphasis>Zend_Session is currently marked as read-only</emphasis>" is thrown while
  336. performing unit tests, if any write-related methods are used after ending the session. However, unit tests
  337. using <classname>Zend_Session</classname> require extra attention, because closing (<methodname>Zend_Session::writeClose()</methodname>), or
  338. destroying a session (<methodname>Zend_Session::destroy()</methodname>) prevents any further setting or unsetting of
  339. keys in any instance of <classname>Zend_Session_Namespace</classname>. This behavior is a direct result of the
  340. underlying ext/session mechanism and <acronym>PHP</acronym>'s <methodname>session_destroy()</methodname> and
  341. <methodname>session_write_close()</methodname>, which have no "undo" mechanism to facilitate setup/teardown with unit
  342. tests.
  343. </para>
  344. <para>
  345. To work around this, see the unit test <methodname>testSetExpirationSeconds()</methodname> in
  346. <code>SessionTest.php</code> and <code>SessionTestHelper.php</code>, both located in
  347. <code>tests/Zend/Session</code>, which make use of <acronym>PHP</acronym>'s <methodname>exec()</methodname> to launch a separate process.
  348. The new process more accurately simulates a second, successive request from a browser. The separate process
  349. begins with a "clean" session, just like any <acronym>PHP</acronym> script execution for a web request. Also, any changes to
  350. <varname>$_SESSION</varname> made in the calling process become available to the child process, provided the
  351. parent closed the session before using <methodname>exec()</methodname>.
  352. </para>
  353. <example id="zend.session.advanced_usage.testing.example">
  354. <title>PHPUnit Testing Code Dependent on Zend_Session</title>
  355. <programlisting language="php"><![CDATA[
  356. // testing setExpirationSeconds()
  357. $script = 'SessionTestHelper.php';
  358. $s = new Zend_Session_Namespace('space');
  359. $s->a = 'apple';
  360. $s->o = 'orange';
  361. $s->setExpirationSeconds(5);
  362. Zend_Session::regenerateId();
  363. $id = Zend_Session::getId();
  364. session_write_close(); // release session so process below can use it
  365. sleep(4); // not long enough for things to expire
  366. exec($script . "expireAll $id expireAll", $result);
  367. $result = $this->sortResult($result);
  368. $expect = ';a === apple;o === orange;p === pear';
  369. $this->assertTrue($result === $expect,
  370. "iteration over default Zend_Session namespace failed; " .
  371. "expecting result === '$expect', but got '$result'");
  372. sleep(2); // long enough for things to expire (total of 6 seconds
  373. // waiting, but expires in 5)
  374. exec($script . "expireAll $id expireAll", $result);
  375. $result = array_pop($result);
  376. $this->assertTrue($result === '',
  377. "iteration over default Zend_Session namespace failed; " .
  378. "expecting result === '', but got '$result')");
  379. session_start(); // resume artificially suspended session
  380. // We could split this into a separate test, but actually, if anything
  381. // leftover from above contaminates the tests below, that is also a
  382. // bug that we want to know about.
  383. $s = new Zend_Session_Namespace('expireGuava');
  384. $s->setExpirationSeconds(5, 'g'); // now try to expire only 1 of the
  385. // keys in the namespace
  386. $s->g = 'guava';
  387. $s->p = 'peach';
  388. $s->p = 'plum';
  389. session_write_close(); // release session so process below can use it
  390. sleep(6); // not long enough for things to expire
  391. exec($script . "expireAll $id expireGuava", $result);
  392. $result = $this->sortResult($result);
  393. session_start(); // resume artificially suspended session
  394. $this->assertTrue($result === ';p === plum',
  395. "iteration over named Zend_Session namespace failed (result=$result)");
  396. ]]></programlisting>
  397. </example>
  398. </sect2>
  399. </sect1>