multiuser-sessions.xml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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
  24. <classname>Zend_Session</classname> adds value to the <acronym>PHP</acronym> session
  25. extension making it easier to use and 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 <classname>Zend_Session</classname> component is both a session manager as well as
  32. an <acronym>API</acronym> for storing data into a session object for long-term
  33. persistence. The <classname>Zend_Session</classname> <acronym>API</acronym> is for
  34. managing the options and behavior of a session, like options, starting and stopping a
  35. session, whereas <classname>Zend_Session_Namespace</classname> is the actual object used
  36. to store data.
  37. </para>
  38. <para>
  39. While its generally good practice to start a session inside a bootstrap process, this
  40. is generally not necessary as all sessions will be automatically started upon the first
  41. creation of a <classname>Zend_Session_Namespace</classname> object.
  42. </para>
  43. <para>
  44. <classname>Zend_Application</classname> is capable of configuring
  45. <classname>Zend_Session</classname> for you as part of the
  46. <classname>Zend_Application_Resource</classname> system. To use this, assuming your
  47. project uses <classname>Zend_Application</classname> to bootstrap, you would add the
  48. following code to your application.ini file:
  49. </para>
  50. <programlisting language="php"><![CDATA[
  51. resources.session.save_path = APPLICATION_PATH "/../data/session"
  52. resources.session.use_only_cookies = true
  53. resources.session.remember_me_seconds = 864000
  54. ]]></programlisting>
  55. <para>
  56. As you can see, the options passed in are the same options that you'd expect to find
  57. in the ext/session extension in <acronym>PHP</acronym>. Those options setup the path
  58. to the session files where data will be stored within the project. Since ini files can
  59. additionally use constants, the above will use the APPLICATION_PATH constant and
  60. relatively point to a data session directory.
  61. </para>
  62. <para>
  63. Most Zend Framework components that use sessions need nothing more to use
  64. <classname>Zend_Session</classname>. At this point, you an either use a component that
  65. consumes <classname>Zend_Session</classname>, or start storing your own data inside a
  66. session with <classname>Zend_Session_Namespace</classname>.
  67. </para>
  68. <para>
  69. <classname>Zend_Session_Namespace</classname> is a simple class that proxies data via an
  70. easy to use <acronym>API</acronym> into the <classname>Zend_Session</classname> managed
  71. $_SESSION superglobal. The reason it is called
  72. <classname>Zend_Session_Namespace</classname> is that it effectively namespaces the data
  73. inside $_SESSION, thus allowing multiple components and objects to safely store and
  74. retrieve data. In the following code, we'll explore how to build a simple session
  75. incrementing counter, starting at 1000 and resetting itself after 1999.
  76. </para>
  77. <programlisting language="php"><![CDATA[
  78. $mysession = new Zend_Session_Namespace('mysession');
  79. if (!isset($mysession->counter)) {
  80. $mysession->counter = 1000;
  81. } else {
  82. $mysession->counter++;
  83. }
  84. if ($mysession->counter > 1999) {
  85. unset($mysession->counter);
  86. }
  87. ]]></programlisting>
  88. <para>
  89. As you can see above, the session namespace object uses the magic __get, __set,
  90. __isset, and __unset to allow you to seamlessly and fluently interact with the session.
  91. The information stored in the above example is stored at
  92. $_SESSION['mysession']['counter'].
  93. </para>
  94. </sect2>
  95. <sect2 id="learning.multiuser.sessions.advanced-usage">
  96. <title>Advanced Usage of Zend_Session</title>
  97. <para>
  98. Additionally, if you wanted to use the DbTable
  99. save handler for <classname>Zend_Session</classname>, you'd add the following code to
  100. your application.ini:
  101. </para>
  102. <programlisting language="php"><![CDATA[
  103. resources.session.saveHandler.class = "Zend_Session_SaveHandler_DbTable"
  104. resources.session.saveHandler.options.name = "session"
  105. resources.session.saveHandler.options.primary.session_id = "session_id"
  106. resources.session.saveHandler.options.primary.save_path = "save_path"
  107. resources.session.saveHandler.options.primary.name = "name"
  108. resources.session.saveHandler.options.primaryAssignment.sessionId = "sessionId"
  109. resources.session.saveHandler.options.primaryAssignment.sessionSavePath = "sessionSavePath"
  110. resources.session.saveHandler.options.primaryAssignment.sessionName = "sessionName"
  111. resources.session.saveHandler.options.modifiedColumn = "modified"
  112. resources.session.saveHandler.options.dataColumn = "session_data"
  113. resources.session.saveHandler.options.lifetimeColumn = "lifetime"
  114. ]]></programlisting>
  115. </sect2>
  116. </sect1>