multiuser-sessions.xml 6.6 KB

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