Skip to content

Commit ae81a2b

Browse files
committed
Added a session handler class for mysql based session storage.
1 parent 12fcb24 commit ae81a2b

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TABLE `sys_session` (
2+
`session_id` varchar(32) NOT NULL default '',
3+
`date_created` datetime NOT NULL default '0000-00-00 00:00:00',
4+
`last_updated` datetime NOT NULL default '0000-00-00 00:00:00',
5+
`session_data` longtext,
6+
PRIMARY KEY (`session_id`),
7+
KEY `last_updated` (`last_updated`)
8+
) ENGINE=MyISAM;

install/sql/ispconfig3.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,21 @@ CREATE TABLE `sys_user` (
10661066

10671067
-- --------------------------------------------------------
10681068

1069+
--
1070+
-- Table structure for table `sys_session`
1071+
--
1072+
1073+
CREATE TABLE `sys_session` (
1074+
`session_id` varchar(32) NOT NULL default '',
1075+
`date_created` datetime NOT NULL default '0000-00-00 00:00:00',
1076+
`last_updated` datetime NOT NULL default '0000-00-00 00:00:00',
1077+
`session_data` longtext,
1078+
PRIMARY KEY (`session_id`),
1079+
KEY `last_updated` (`last_updated`)
1080+
) ENGINE=MyISAM;
1081+
1082+
-- --------------------------------------------------------
1083+
10691084
--
10701085
-- Table structure for table `web_domain`
10711086
--

interface/lib/app.inc.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ public function __construct() {
5656

5757
//* Start the session
5858
if($this->_conf['start_session'] == true) {
59+
60+
$this->uses('session');
61+
session_set_save_handler( array($this->session, 'open'),
62+
array($this->session, 'close'),
63+
array($this->session, 'read'),
64+
array($this->session, 'write'),
65+
array($this->session, 'destroy'),
66+
array($this->session, 'gc'));
67+
5968
session_start();
6069

6170
//* Initialize session variables
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2010, Till Brehm, projektfarm Gmbh
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
class session {
32+
33+
private $session_array = array();
34+
private $db;
35+
36+
function __construct() {
37+
$this->db = new db;
38+
}
39+
40+
function open ($save_path, $session_name) {
41+
return true;
42+
}
43+
44+
function close () {
45+
if (!empty($this->fieldarray)) {
46+
$result = $this->gc(ini_get('session.gc_maxlifetime'));
47+
return $result;
48+
}
49+
return false;
50+
}
51+
52+
function read ($session_id) {
53+
54+
$rec = $this->db->queryOneRecord("SELECT * FROM sys_session WHERE session_id = '".$this->db->quote($session_id)."'");
55+
56+
if (is_array($rec)) {
57+
$this->session_array = $rec;
58+
return $this->session_array['session_data'];
59+
} else {
60+
return '';
61+
}
62+
}
63+
64+
function write ($session_id, $session_data) {
65+
66+
if (!empty($this->session_array) && $this->session_array['session_id'] != $session_id) {
67+
$this->session_array = array();
68+
}
69+
70+
if ($this->session_array['session_id'] == '') {
71+
$session_id = $this->db->quote($session_id);
72+
$date_created = date('Y-m-d H:i:s');
73+
$last_updated = date('Y-m-d H:i:s');
74+
$session_data = $this->db->quote($session_data);
75+
$sql = "INSERT INTO sys_session (session_id,date_created,last_updated,session_data) VALUES ('$session_id','$date_created','$last_updated','$session_data')";
76+
$this->db->query($sql);
77+
} else {
78+
$session_id = $this->db->quote($session_id);
79+
$last_updated = date('Y-m-d H:i:s');
80+
$session_data = $this->db->quote($session_data);
81+
$sql = "UPDATE sys_session SET last_updated = '$last_updated', session_data = '$session_data' WHERE session_id = '$session_id'";
82+
$this->db->query($sql);
83+
}
84+
85+
return true;
86+
}
87+
88+
function destroy ($session_id) {
89+
90+
$session_id = $this->db->quote($session_id);
91+
$sql = "DELETE FROM sys_session WHERE session_id = '$session_id'";
92+
$this->db->query($sql);
93+
94+
return true;
95+
}
96+
97+
function gc ($max_lifetime) {
98+
99+
$real_now = date('Y-m-d H:i:s');
100+
$dt1 = strtotime("$real_now -$max_lifetime seconds");
101+
$dt2 = date('Y-m-d H:i:s', $dt1);
102+
103+
$sql = "DELETE FROM sys_session WHERE last_updated < '$dt2'";
104+
$this->db->query($sql);
105+
106+
return true;
107+
108+
}
109+
110+
function __destruct () {
111+
@session_write_close();
112+
113+
}
114+
}
115+
116+
?>

0 commit comments

Comments
 (0)