multiuser-sessions.xml 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 19777 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="learning.multiuser.sessions">
  5. <title>Managing User Sessions In ZF</title>
  6. <sect2 id="learning.multiuser.sessions.intro">
  7. <title>Introduction to Sessions</title>
  8. <para>
  9. The success of the web is deeply rooted in the protocol that drives the web: HTTP. HTTP
  10. over TCP is by its very nature stateless, which means that inherently the web is also
  11. stateless. While this very aspect is one of the dominating factors for why the web has
  12. become such a popular medium, it also causes an interesting problem for developers that
  13. want to use the web as an application platform.
  14. </para>
  15. <para>
  16. The act of interacting with a web application is typically defined by the sum
  17. of all requests sent to a web server. Since there can be many consumers being served
  18. simultaneously, the application must decide which requests belong to which consumer. These
  19. requests are typically known as a "session".
  20. </para>
  21. <para>
  22. In PHP, the session problem is solved by the session extension which utilizes some state
  23. tracking, typically cookies, and some form of local storage which is exposed via the
  24. $_SESSION superglobal. In Zend Framework, the component Zend_Session adds value to the php
  25. session extension making it easier to use and depend on inside object-oriented
  26. applications.
  27. </para>
  28. </sect2>
  29. <sect2 id="learning.multiuser.sessions.basic-usage">
  30. <title>Basic Usage of Zend_Session</title>
  31. <para>
  32. The Zend_Session component is both a session manager as well as an API for
  33. storing data into a session object for long-term persistence. The Zend_Session API is
  34. for managing the options and behavior of a session, like options, starting and stopping
  35. a session, whereas Zend_Session_Namespace is the actual object used to store data.
  36. </para>
  37. <para>
  38. While its generally good practice to start a session inside a bootstrap process, this
  39. is generally not necessary as all sessions will be automatically started upon the first
  40. creation of a Zend_Session_Namespace object.
  41. </para>
  42. <para>
  43. Zend_Application is capable of configuring Zend_Session for you as part of the
  44. Zend_Application_Resource system. To use this, assuming your project uses
  45. Zend_Application to bootstrap, you would add the following code to your
  46. application.ini file:
  47. </para>
  48. <programlisting language="php"><![CDATA[
  49. resources.session.save_path = APPLICATION_PATH "/../data/session"
  50. resources.session.use_only_cookies = true
  51. resources.session.remember_me_seconds = 864000
  52. ]]></programlisting>
  53. <para>
  54. As you can see, the options passed in are the same options that you'd expect to find
  55. in the ext/session extension in PHP. Those options setup the path to the session
  56. files where data will be stored within the project. Since ini files can additionally
  57. use constants, the above will use the APPLICATION_PATH constant and relatively point
  58. to a data session directory.
  59. </para>
  60. <para>
  61. Most Zend Framework components that use sessions need nothing more to use Zend_Session.
  62. At this point, you an either use a component that consumes Zend_Session, or start
  63. storing your own data inside a session with Zend_Session_Namespace.
  64. </para>
  65. <para>
  66. Zend_Session_Namespace is a simple class that proxies data via an easy to use API
  67. into the Zend_Session managed $_SESSION superglobal. The reason it is called
  68. Zend_Session_Namespace is that it effectively namespaces the data inside $_SESSION, thus
  69. allowing multiple components and objects to safely store and retrieve data. In the
  70. following code, we'll explore how to build a simple session incrementing counter, starting
  71. at 1000 and resetting itself after 1999.
  72. </para>
  73. <programlisting language="php"><![CDATA[
  74. $mysession = Zend_Session_Namespace('mysession');
  75. if (!isset($mysession->counter)) {
  76. $mysession->counter = 1000;
  77. } else {
  78. $mysession->counter++;
  79. }
  80. if ($mysession->counter > 1999) {
  81. unset($mysession->counter);
  82. }
  83. ]]></programlisting>
  84. <para>
  85. As you can see above, the session namespace object uses the magic __get, __set,
  86. __isset, and __unset to allow you to seemlessly and fluently interact with the session.
  87. The information stored in the above example is stored at $_SESSION['mysession']['counter'].
  88. </para>
  89. </sect2>
  90. <sect2 id="learning.multiuser.sessions.advanced-usage">
  91. <title>Advanced Usage of Zend_Session</title>
  92. <para>
  93. Addionally, if you wanted to use the DbTable
  94. save handler for Zend_Session, you'd add the following code to your application.ini:
  95. </para>
  96. <programlisting language="php"><![CDATA[
  97. resources.session.saveHandler.class = "Zend_Session_SaveHandler_DbTable"
  98. resources.session.saveHandler.options.name = "session"
  99. resources.session.saveHandler.options.primary.session_id = "session_id"
  100. resources.session.saveHandler.options.primary.save_path = "save_path"
  101. resources.session.saveHandler.options.primary.name = "name"
  102. resources.session.saveHandler.options.primaryAssignment.sessionId = "sessionId"
  103. resources.session.saveHandler.options.primaryAssignment.sessionSavePath = "sessionSavePath"
  104. resources.session.saveHandler.options.primaryAssignment.sessionName = "sessionName"
  105. resources.session.saveHandler.options.modifiedColumn = "modified"
  106. resources.session.saveHandler.options.dataColumn = "session_data"
  107. resources.session.saveHandler.options.lifetimeColumn = "lifetime"
  108. ]]></programlisting>
  109. </sect2>
  110. </sect1>