Zend_Session-SaveHandler-DbTable.xml 3.9 KB

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