Witam.
Piszę własną obsługę sesji. Już to kiedyś przerabiałem i mam nawet klasę która się tym zajmuje.
Jednak odświeżając wiedzę na ten temat trafiłem na sprzeczność. Mianowicie podobno prawie wszystkie handlery muszą zwracać bolowskie true.
Natomiast w mojej klasie która pisałem jakieś półtora roku temu owe handlery zwracają niekiedy różne wartości ( przeważnie liczbowe )
Wiem że zostaną one przekonwertowane do wartości logicznych, ale w takim razie po diabła zwracać cokolwiek oprócz true skoro na stronie:
http://www.hudzilla.org/phpbook/read.php/10_3_7
Cytat
All the functions return true except sess_read()

pragnę dodać że tą klasę mimo iż pisałem ją dosyć dawno temu to z książką w łapie - niestety tytułu nie pamiętam.
Więc jak to jest z tym wartościami zwracanymi?

- Przy okazji prosiłbym o hmm zerknięcie na kod klasy Session i wyrażenie swojej opinii
  1. <?php
  2. class Session
  3. {
  4. /**
  5.  * @var DDHandler
  6.  */
  7. protected $db;
  8. /**
  9.    * Used to store unserialized session data
  10.    * @var array
  11.    */
  12. protected $_sess_data;
  13. /**
  14.    * Class Constructor
  15.    * @return Session
  16.    */
  17. public function __construct()
  18. {
  19. $this->db = DB::getDriver();
  20.  array(&$this, '_open'),
  21.  array(&$this, '_close'),
  22.  array(&$this, '_read'),
  23.  array(&$this, '_write'),
  24.  array(&$this, '_destroy'),
  25.  array(&$this, '_gc')
  26.  );
  27. if(!$ok) {
  28. Event::trigger('ERR_SESSION_SET_SAVE_HANDLER_FAILED');
  29. }
  30. // Set random _gc() method to be triggered every time
  31. ini_set('session.gc_divisor', 1000);
  32. ini_set('session.gc_probability', 1000);
  33. }
  34. /**
  35.    * Called by session_start()
  36.    * @param string $save_path
  37.    * @param string $session_name
  38.    * @return boolean true
  39.    */
  40. public function _open($save_path, $session_name)
  41. {
  42. return true;
  43. }
  44. /**
  45.    * Called at page end
  46.    * @return integer
  47.    */
  48. public function _close()
  49. {
  50. return true;
  51. }
  52. /**
  53.    * Called after session_start()
  54.    * Have to return serializaed session data or empty string
  55.    * Reads session data automaticly when class is created and saves all data to
  56.    * local variable accessed with methods remove(), set() and get()
  57.    * @param integer $session_id
  58.    * @return string
  59.    */
  60. public function _read($session_id)
  61. {
  62. $result = $this->db->query('SELECT * FROM '.DB_PREFIX.'session WHERE sess_id=''.$session_id.''');
  63. if($result->columnCount() > 0)
  64. {
  65. $session_data = $result->fetch();
  66. $this->_sess_data = unserialize($session_data['sess_value']);
  67. foreach($session_data as $key => $value)
  68. {
  69. $this->_sess_data[$key] = $value;
  70. }
  71. $out = $session_data['sess_value'];
  72. } else {
  73. $out = '';
  74. }
  75. return $out;
  76. }
  77. /**
  78.    * Called when session data is to be written
  79.    * Always returns true
  80.    * @param integer $session_id
  81.    * @param string $session_data
  82.    * @return boolean
  83.    */
  84. public function _write($session_id, $session_data)
  85. {
  86. $row = $this->db->query('SELECT COUNT(*) as count FROM '.DB_PREFIX.'session WHERE sess_id=''.$session_id.''')->fetch();
  87. if($row['count'] > 0)
  88. {
  89. $result = $this->db->query('UPDATE '.DB_PREFIX.'session SET 
  90. sess_last_update=CURRENT_TIMESTAMP(),
  91. sess_value=''.$session_data.'' WHERE sess_id=''.$session_id.''');
  92. $out = $this->db->affectedRows() > 0 ? true : false;
  93. } 
  94. else 
  95. {
  96. if(isset($this->_sess_data['user_id'])) {
  97. $user_id = $this->_sess_data['user_id'];
  98. } else {
  99. $user_id = 0;
  100. }
  101. $result = $this->db->query('INSERT INTO '.DB_PREFIX.'session 
  102. (sess_id, sess_start, sess_last_update, sess_value, user_id, user_ip) 
  103. VALUES(''.$session_id.'', CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), ''.$session_data.''''.$user_id.''''.$_SERVER['REMOTE_ADDR'].'')');
  104. $out = $this->db->affectedRows() > 0 ? true : false;
  105. }
  106. return $out;
  107. }
  108. /**
  109.    * Called by session_destroy()
  110.    * @param integer $session_id
  111.    * @return boolean
  112.    */
  113. public function _destroy($session_id)
  114. {
  115. $result = $this->db->query('DELETE FROM '.DB_PREFIX.'session WHERE sess_id=''.$session_id.''');
  116. return $this->db->affectedRows() > 0 ? true : false;
  117. }
  118. /**
  119.    * Called randomly
  120.    * Removes stored sessions which last update is longer than $maxlifetime param
  121.    * @param integer $maxlifetime
  122.    * @return integer
  123.    */
  124. public function _gc($maxlifetime)
  125. {
  126. $sess_live_time = strftime('%Y-%m-%d %H:%M', strtotime("-$maxlifetime seconds"));
  127. $this->db->query('DELETE FROM '.DB_PREFIX.'session WHERE sess_last_update < ''.$sess_live_time.''');
  128. return $this->db->affectedRows();
  129. }
  130. // ----------------- //
  131. // SETTERS & GETTERS //
  132. // ----------------- //
  133. /**
  134.    * Removes stored session data
  135.    * @param string $session_variable_id
  136.    */
  137. public function remove($session_variable_id) 
  138. {
  139. if(isset($this->_sess_data[$session_variable_id])) {
  140. unset($this->_sess_data[$session_variable_id]);
  141. }
  142. }
  143. /**
  144.    * Creates new session variable or changes the value of existing one
  145.    * @param string $session_variable_id
  146.    * @param misc $value
  147.    */
  148. public function set($session_variable_id, $value)
  149. {
  150. $this->_sess_data[$session_variable_id] = $value;
  151. }
  152. /**
  153.    * Returns session variable identified by $session_variable_id
  154.    * @param string $session_variable_id
  155.    * @return misc
  156.    */
  157. public function get($session_variable_id)
  158. {
  159. if(isset($this->_sess_data[$session_variable_id])) {
  160. return $this->_sess_data[$session_variable_id];
  161. }
  162. }
  163. ?>