123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- class sfMySQLSessionStorage extends sfDatabaseSessionStorage
- {
-
- public function sessionDestroy($id)
- {
-
- $db_table = $this->options['db_table'];
- $db_id_col = $this->options['db_id_col'];
-
- $id = $this->db_escape($id);
-
- $sql = "DELETE FROM $db_table WHERE $db_id_col = '$id'";
- if ($this->db_query($sql))
- {
- return true;
- }
-
- throw new sfDatabaseException(sprintf('%s cannot destroy session id "%s" (%s).', get_class($this), $id, mysql_error()));
- }
-
- public function sessionGC($lifetime)
- {
-
- $db_table = $this->options['db_table'];
- $db_time_col = $this->options['db_time_col'];
-
- $lifetime = $this->db_escape($lifetime);
- $sql = "DELETE FROM $db_table WHERE $db_time_col + $lifetime < UNIX_TIMESTAMP()";
- if (!$this->db_query($sql))
- {
- throw new sfDatabaseException(sprintf('%s cannot delete old sessions (%s).', get_class($this), mysql_error()));
- }
- return true;
- }
-
- public function sessionRead($id)
- {
-
- $db_table = $this->options['db_table'];
- $db_data_col = $this->options['db_data_col'];
- $db_id_col = $this->options['db_id_col'];
- $db_time_col = $this->options['db_time_col'];
-
- $id = $this->db_escape($id);
-
- $sql = "SELECT $db_data_col FROM $db_table WHERE $db_id_col = '$id'";
- $result = $this->db_query($sql);
- if ($result != false && $this->db_num_rows($result) == 1)
- {
-
- $data = $this->db_fetch_row($result);
- return $data[0];
- }
- else
- {
-
- $sql = "INSERT INTO $db_table ($db_id_col, $db_data_col, $db_time_col) VALUES ('$id', '', UNIX_TIMESTAMP())";
- if ($this->db_query($sql))
- {
- return '';
- }
-
- throw new sfDatabaseException(sprintf('%s cannot create new record for id "%s" (%s).', get_class($this), $id, mysql_error()));
- }
- }
-
- public function sessionWrite($id, $data)
- {
-
- $db_table = $this->options['db_table'];
- $db_data_col = $this->options['db_data_col'];
- $db_id_col = $this->options['db_id_col'];
- $db_time_col = $this->options['db_time_col'];
-
- $id = $this->db_escape($id);
- $data = $this->db_escape($data);
-
- $sql = "UPDATE $db_table SET $db_data_col='$data', $db_time_col=UNIX_TIMESTAMP() WHERE $db_id_col='$id'";
- if ($this->db_query($sql))
- {
- return true;
- }
-
- throw new sfDatabaseException(sprintf('%s cannot write session data for id "%s" (%s).', get_class($this), $id, mysql_error()));
- }
-
- protected function db_query($query)
- {
- return @mysql_query($query, $this->db);
- }
-
- protected function db_escape($string)
- {
- return mysql_real_escape_string($string, $this->db);
- }
-
- protected function db_num_rows($result)
- {
- return mysql_num_rows($result);
- }
-
- protected function db_fetch_row($result)
- {
- return mysql_fetch_row($result);
- }
- }
|