Development

/branches/1.1/lib/storage/sfPDOSessionStorage.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/storage/sfPDOSessionStorage.class.php

Revision 10589, 4.9 kB (checked in by nicolas, 6 years ago)

[1.1] fixed old session not destroyed when changing authentication state (closes #3927)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004, 2005 Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) 2004, 2005 Sean Kerr <sean@code-box.org>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * Provides support for session storage using a PDO database abstraction layer.
14  *
15  * <b>parameters:</b> see sfDatabaseSessionStorage
16  *
17  * @package    symfony
18  * @subpackage storage
19  * @author     Mathew Toth <developer@poetryleague.com>
20  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
21  * @author     Sean Kerr <sean@code-box.org>
22  * @version    SVN: $Id$
23  */
24 class sfPDOSessionStorage extends sfDatabaseSessionStorage
25 {
26   /**
27    * Destroys a session.
28    *
29    * @param  string $id  A session ID
30    *
31    * @return bool true, if the session was destroyed, otherwise an exception is thrown
32    *
33    * @throws <b>DatabaseException</b> If the session cannot be destroyed
34    */
35   public function sessionDestroy($id)
36   {
37     // get table/column
38     $db_table  = $this->options['db_table'];
39     $db_id_col = $this->options['db_id_col'];
40
41     // delete the record associated with this id
42     $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.'= ?';
43
44     try
45     {
46       $stmt = $this->db->prepare($sql);
47       $stmt->bindParam(1, $id, PDO::PARAM_STR);
48       $stmt->execute();
49     }
50     catch (PDOException $e)
51     {
52       throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
53     }
54     
55     return true;
56   }
57
58   /**
59    * Cleans up old sessions.
60    *
61    * @param  int $lifetime  The lifetime of a session
62    *
63    * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
64    *
65    * @throws <b>DatabaseException</b> If any old sessions cannot be cleaned
66    */
67   public function sessionGC($lifetime)
68   {
69     // get table/column
70     $db_table    = $this->options['db_table'];
71     $db_time_col = $this->options['db_time_col'];
72
73     // delete the record associated with this id
74     $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime);
75
76     try
77     {
78       $this->db->query($sql);
79     }
80     catch (PDOException $e)
81     {
82       throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
83     }
84
85     return true;
86   }
87
88   /**
89    * Reads a session.
90    *
91    * @param  string $id  A session ID
92    *
93    * @return string      The session data if the session was read or created, otherwise an exception is thrown
94    *
95    * @throws <b>DatabaseException</b> If the session cannot be read
96    */
97   public function sessionRead($id)
98   {
99     // get table/columns
100     $db_table    = $this->options['db_table'];
101     $db_data_col = $this->options['db_data_col'];
102     $db_id_col   = $this->options['db_id_col'];
103     $db_time_col = $this->options['db_time_col'];
104
105     try
106     {
107       $sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.'=?';
108
109       $stmt = $this->db->prepare($sql);
110       $stmt->bindParam(1, $id, PDO::PARAM_STR, 255);
111
112       $stmt->execute();
113       if ($data = $stmt->fetchColumn())
114       {
115         return $data;
116       }
117       else
118       {
119         // session does not exist, create it
120         $sql = 'INSERT INTO '.$db_table.'('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (?, ?, ?)';
121
122         $stmt = $this->db->prepare($sql);
123         $stmt->bindParam(1, $id, PDO::PARAM_STR);
124         $stmt->bindValue(2, '', PDO::PARAM_STR);
125         $stmt->bindValue(3, time(), PDO::PARAM_INT);
126         $stmt->execute();
127
128         return '';
129       }
130     }
131     catch (PDOException $e)
132     {
133       throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
134     }
135   }
136
137   /**
138    * Writes session data.
139    *
140    * @param  string $id    A session ID
141    * @param  string $data  A serialized chunk of session data
142    *
143    * @return bool true, if the session was written, otherwise an exception is thrown
144    *
145    * @throws <b>DatabaseException</b> If the session data cannot be written
146    */
147   public function sessionWrite($id, $data)
148   {
149     // get table/column
150     $db_table    = $this->options['db_table'];
151     $db_data_col = $this->options['db_data_col'];
152     $db_id_col   = $this->options['db_id_col'];
153     $db_time_col = $this->options['db_time_col'];
154
155     $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = ?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'= ?';
156
157     try
158     {
159       $stmt = $this->db->prepare($sql);
160       $stmt->bindParam(1, $data, PDO::PARAM_STR);
161       $stmt->bindParam(2, $id, PDO::PARAM_STR);
162       $stmt->execute();
163     }
164     catch (PDOException $e)
165     {
166       throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
167     }
168
169     return true;
170   }
171 }
172
Note: See TracBrowser for help on using the browser.