1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Kotchasan;
12:
13: class Session extends \Kotchasan\Model
14: {
15: 16: 17:
18: private $database;
19: 20: 21:
22: private $table;
23:
24: 25: 26:
27: public function _open()
28: {
29: $model = new static;
30: $this->database = $model->db();
31: $this->table = $model->getTableName('sessions');
32: return true;
33: }
34:
35: 36: 37:
38: public function _close()
39: {
40: return true;
41: }
42:
43: 44: 45: 46: 47:
48: public function _read($sess_id)
49: {
50: $search = $this->database->first($this->table, array('sess_id', $sess_id), true);
51: if ($search) {
52: return $search->data;
53: } else {
54: return '';
55: }
56: }
57:
58: 59: 60: 61: 62: 63:
64: public function _write($sess_id, $data)
65: {
66: $search = $this->database->first($this->table, array('sess_id', $sess_id), true);
67: if ($search) {
68: $this->database->update($this->table, array('sess_id', $sess_id), array(
69: 'access' => time(),
70: 'data' => $data,
71: ));
72: } else {
73: $this->database->insert($this->table, array(
74: 'sess_id' => $sess_id,
75: 'access' => time(),
76: 'data' => $data,
77: 'create_date' => date('Y-m-d H:i:s'),
78: ));
79: }
80: return true;
81: }
82:
83: 84: 85: 86: 87:
88: public function _destroy($sess_id)
89: {
90: $this->database->delete($this->table, array('sess_id', $sess_id));
91: return true;
92: }
93:
94: 95: 96: 97: 98:
99: public function _gc($max)
100: {
101: $old = time() - $max;
102: $this->database->delete($this->table, array('access', '<', $old));
103: return true;
104: }
105: }
106: