1. sfPDOSessionStorage.class.php
  2. /** * Provides support for session storage using a PDO database abstraction layer. * * parameters: see sfDatabaseSessionStorage * * @package symfony * @subpackage storage * @author Mathew Toth * @author Fabien Potencier * @author Sean Kerr * @version SVN: $Id: sfPDOSessionStorage.class.php 13143 2008-11-18 22:22:01Z FabianLange $ */
  3. class sfPDOSessionStorage extends sfDatabaseSessionStorage
  4. {
  5. /**
  6. * Destroys a session.
  7. *
  8. * @param string $id A session ID
  9. *
  10. * @return bool true, if the session was destroyed, otherwise an exception is thrown
  11. *
  12. * @throws <b>DatabaseException</b> If the session cannot be destroyed
  13. */
  14. public function sessionDestroy($id)
  15. {
  16. // get table/column
  17. $db_table = $this->options['db_table'];
  18. $db_id_col = $this->options['db_id_col'];
  19. // delete the record associated with this id
  20. $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.'= ?';
  21. try
  22. {
  23. $stmt = $this->db->prepare($sql);
  24. $stmt->bindParam(1, $id, PDO::PARAM_STR);
  25. $stmt->execute();
  26. }
  27. catch (PDOException $e)
  28. {
  29. throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
  30. }
  31. return true;
  32. }
  33. /**
  34. * Cleans up old sessions.
  35. *
  36. * @param int $lifetime The lifetime of a session
  37. *
  38. * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
  39. *
  40. * @throws <b>DatabaseException</b> If any old sessions cannot be cleaned
  41. */
  42. public function sessionGC($lifetime)
  43. {
  44. // get table/column
  45. $db_table = $this->options['db_table'];
  46. $db_time_col = $this->options['db_time_col'];
  47. // delete the record associated with this id
  48. $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime);
  49. try
  50. {
  51. $this->db->query($sql);
  52. }
  53. catch (PDOException $e)
  54. {
  55. throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
  56. }
  57. return true;
  58. }
  59. /**
  60. * Reads a session.
  61. *
  62. * @param string $id A session ID
  63. *
  64. * @return string The session data if the session was read or created, otherwise an exception is thrown
  65. *
  66. * @throws <b>DatabaseException</b> If the session cannot be read
  67. */
  68. public function sessionRead($id)
  69. {
  70. // get table/columns
  71. $db_table = $this->options['db_table'];
  72. $db_data_col = $this->options['db_data_col'];
  73. $db_id_col = $this->options['db_id_col'];
  74. $db_time_col = $this->options['db_time_col'];
  75. try
  76. {
  77. $sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.'=?';
  78. $stmt = $this->db->prepare($sql);
  79. $stmt->bindParam(1, $id, PDO::PARAM_STR, 255);
  80. $stmt->execute();
  81. // it is recommended to use fetchAll so that PDO can close the DB cursor
  82. // we anyway expect either no rows, or one row with one column. fetchColumn, seems to be buggy #4777
  83. $sessionRows = $stmt->fetchAll(PDO::FETCH_NUM);
  84. if (count($sessionRows) == 1)
  85. {
  86. return $sessionRows[0][0];
  87. }
  88. else
  89. {
  90. // session does not exist, create it
  91. $sql = 'INSERT INTO '.$db_table.'('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (?, ?, ?)';
  92. $stmt = $this->db->prepare($sql);
  93. $stmt->bindParam(1, $id, PDO::PARAM_STR);
  94. $stmt->bindValue(2, '', PDO::PARAM_STR);
  95. $stmt->bindValue(3, time(), PDO::PARAM_INT);
  96. $stmt->execute();
  97. return '';
  98. }
  99. }
  100. catch (PDOException $e)
  101. {
  102. throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
  103. }
  104. }
  105. /**
  106. * Writes session data.
  107. *
  108. * @param string $id A session ID
  109. * @param string $data A serialized chunk of session data
  110. *
  111. * @return bool true, if the session was written, otherwise an exception is thrown
  112. *
  113. * @throws <b>DatabaseException</b> If the session data cannot be written
  114. */
  115. public function sessionWrite($id, $data)
  116. {
  117. // get table/column
  118. $db_table = $this->options['db_table'];
  119. $db_data_col = $this->options['db_data_col'];
  120. $db_id_col = $this->options['db_id_col'];
  121. $db_time_col = $this->options['db_time_col'];
  122. $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = ?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'= ?';
  123. try
  124. {
  125. $stmt = $this->db->prepare($sql);
  126. $stmt->bindParam(1, $data, PDO::PARAM_STR);
  127. $stmt->bindParam(2, $id, PDO::PARAM_STR);
  128. $stmt->execute();
  129. }
  130. catch (PDOException $e)
  131. {
  132. throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
  133. }
  134. return true;
  135. }
  136. }

Debug toolbar