2
0

Zend_Session-SaveHandler-DbTable.xml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.session.savehandler.dbtable">
  4. <title>Zend_Session_SaveHandler_DbTable</title>
  5. <para>
  6. The basic setup for Zend_Session_SaveHandler_DbTable must at least
  7. have four columns, denoted in the config array/Zend_Config object:
  8. primary, which is the primary key and defaults to just the session
  9. id which by default is a string of length 32;
  10. modified, which is the unix timestamp of the last modified date;
  11. lifetime, which is the lifetime of the session (modified + lifetime > time());
  12. and data, which is the serialized data stored in the session
  13. </para>
  14. <example id="zend.session.savehandler.dbtable.basic">
  15. <title>Basic Setup</title>
  16. <programlisting language="SQL"><![CDATA[
  17. CREATE TABLE `session` (
  18. `id` char(32),
  19. `modified` int,
  20. `lifetime` int,
  21. `data` text,
  22. PRIMARY KEY (`id`)
  23. );
  24. ]]></programlisting>
  25. <programlisting language="php"><![CDATA[
  26. //get your database connection ready
  27. $db = Zend_Db::factory('Pdo_Mysql', array(
  28. 'host' =>'example.com',
  29. 'username' => 'dbuser',
  30. 'password' => '******',
  31. 'dbname' => 'dbname'
  32. ));
  33. //you can either set the Zend_Db_Table default adapter
  34. //or you can pass the db connection straight to the save handler $config
  35. Zend_Db_Table_Abstract::setDefaultAdapter($db);
  36. $config = array(
  37. 'name' => 'session',
  38. 'primary' => 'id',
  39. 'modifiedColumn' => 'modified',
  40. 'dataColumn' => 'data',
  41. 'lifetimeColumn' => 'lifetime'
  42. );
  43. //create your Zend_Session_SaveHandler_DbTable and
  44. //set the save handler for Zend_Session
  45. Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));
  46. //start your session!
  47. Zend_Session::start();
  48. //now you can use Zend_Session like any other time
  49. ]]></programlisting>
  50. </example>
  51. <para>
  52. You can also use Multiple Columns in your primary key for
  53. Zend_Session_SaveHandler_DbTable.
  54. </para>
  55. <example id="zend.session.savehandler.dbtable.multi-column-key">
  56. <title>Using a Multi-Column Primary Key</title>
  57. <programlisting language="SQL"><![CDATA[
  58. CREATE TABLE `session` (
  59. `session_id` char(32) NOT NULL,
  60. `save_path` varchar(32) NOT NULL,
  61. `name` varchar(32) NOT NULL DEFAULT '',
  62. `modified` int,
  63. `lifetime` int,
  64. `session_data` text,
  65. PRIMARY KEY (`Session_ID`, `save_path`, `name`)
  66. );
  67. ]]></programlisting>
  68. <programlisting language="php"><![CDATA[
  69. //setup your DB connection like before
  70. //NOTE: this config is also passed to Zend_Db_Table so anything specific
  71. //to the table can be put in the config as well
  72. $config = array(
  73. 'name' => 'session', //table name as per Zend_Db_Table
  74. 'primary' => array(
  75. 'session_id', //the sessionID given by PHP
  76. 'save_path', //session.save_path
  77. 'name', //session name
  78. ),
  79. 'primaryAssignment' => array(
  80. //you must tell the save handler which columns you
  81. //are using as the primary key. ORDER IS IMPORTANT
  82. 'sessionId', //first column of the primary key is of the sessionID
  83. 'sessionSavePath', //second column of the primary key is the save path
  84. 'sessionName', //third column of the primary key is the session name
  85. ),
  86. 'modifiedColumn' => 'modified', //time the session should expire
  87. 'dataColumn' => 'session_data', //serialized data
  88. 'lifetimeColumn' => 'lifetime', //end of life for a specific record
  89. );
  90. //Tell Zend_Session to use your Save Handler
  91. Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));
  92. //start your session
  93. Zend_Session::start();
  94. //use Zend_Session as normal
  95. ]]></programlisting>
  96. </example>
  97. </sect1>
  98. <!--
  99. vim:se ts=4 sw=4 et:
  100. -->