multiuser-sessions.xml 6.0 KB

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