Zend_Session-AdvancedUsage.xml 23 KB

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