Added contact offers.
[readifood.git] / lib / SessionHandler.php
1 <?php namespace JL;
2
3 /**
4  * A PHP session handler using PDO to keep session data within a MySQL database
5  *
6  * @author  Jan Lohage <info@j2l4e.de>
7  * @link    https://github.com/j2L4e/PHP-PDO-MySQL-Session-Handler
8  *
9  *
10  * Based on PHP-MySQL-Session-Handler (uses mysqli)
11  *
12  * @author  Manuel Reinhard <manu@sprain.ch>
13  * @link    https://github.com/sprain/PHP-MySQL-Session-Handler
14  */
15
16 use PDO;
17 use SessionHandlerInterface;
18
19 class SessionHandler implements SessionHandlerInterface
20 {
21   /**
22    * a PDO connection resource
23    * @var resource
24    */
25   protected $dbh;
26
27
28   /**
29    * the name of the DB table which handles the sessions
30    * @var string
31    */
32   protected $dbTable;
33
34
35   /**
36    * Set db data if no connection is being injected
37    * @param  string $dbHost
38    * @param  string $dbUser
39    * @param  string $dbPassword
40    * @param  string $dbDatabase
41    * @param  string $dbCharset optional, default 'utf8'
42    */
43   public function setDbDetails($dbHost, $dbUser, $dbPassword, $dbDatabase, $dbCharset = 'utf8') {
44
45     //create db connection
46     $this->dbh = new PDO("mysql:" .
47       "host={$dbHost};" .
48       "dbname={$dbDatabase};" .
49       "charset={$dbCharset}",
50       $dbUser,
51       $dbPassword,
52       array(
53         PDO::ATTR_EMULATE_PREPARES => false,
54         PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION //USE ERRMODE_SILENT FOR PRODUCTION!
55       )
56     );
57   }//function
58
59
60   /**
61    * Inject PDO from outside
62    * @param object $dbh expects PDO object
63    */
64   public function setPDO($dbh) {
65     $this->dbh = $dbh;
66   }
67
68
69   /**
70    * Set MySQL table to work with
71    * @param string $dbTable
72    */
73   public function setDbTable($dbTable) {
74     $this->dbTable = $dbTable;
75   }
76
77
78   /**
79    * Open the session
80    * @return bool
81    */
82   public function open($save_path, $session_name) {
83     //delete old session handlers
84     $limit = time() - (3600 * 24);
85     $stmt = $this->dbh->prepare("DELETE FROM {$this->dbTable} WHERE timestamp < :limit");
86     $ret = $stmt->execute(array(':limit' => $limit));
87
88     return $ret;
89   }
90
91   /**
92    * Close the session
93    * @return bool
94    */
95   public function close() {
96     $this->dbh = null;
97   }
98
99   /**
100    * Read the session
101    * @param int session id
102    * @return string string of the sessoin
103    */
104   public function read($id) {
105     $stmt = $this->dbh->prepare("SELECT * FROM {$this->dbTable} WHERE id=:id");
106     $stmt->execute(array(':id' => $id));
107
108     $session = $stmt->fetch(PDO::FETCH_ASSOC);
109
110     if ($session) {
111       $ret = $session['data'];
112     } else {
113       $ret = false;
114     }
115
116     return $ret;
117   }
118
119
120   /**
121    * Write the session
122    * @param int session id
123    * @param string data of the session
124    */
125   public function write($id, $data) {
126     $stmt = $this->dbh->prepare("REPLACE INTO {$this->dbTable} (id,data,timestamp) VALUES (:id,:data,:timestamp)");
127     $ret = $stmt->execute(
128       array(':id' => $id,
129         ':data' => $data,
130         'timestamp' => time()
131       ));
132
133     return $ret;
134   }
135
136   /**
137    * Destroy the session
138    * @param int session id
139    * @return bool
140    */
141   public function destroy($id) {
142     $stmt = $this->dbh->prepare("DELETE FROM {$this->dbTable} WHERE id=:id");
143     $ret = $stmt->execute(array(
144       ':id' => $id
145     ));
146
147     return $ret;
148   }
149
150
151   /**
152    * Garbage Collector
153    * @param int life time (sec.)
154    * @return bool
155    * @see session.gc_divisor      100
156    * @see session.gc_maxlifetime 1440
157    * @see session.gc_probability    1
158    * @usage execution rate 1/100
159    *        (session.gc_probability/session.gc_divisor)
160    */
161   public function gc($max) {
162     $stmt = $this->dbh->prepare("DELETE FROM {$this->dbTable} WHERE timestamp < :limit");
163     $ret = $stmt->execute(array(':limit' => time() - intval($max)));
164
165     return $ret;
166   }
167
168 }//class