Skip to content

Commit 91cac82

Browse files
committed
first commit
0 parents commit 91cac82

File tree

5 files changed

+600
-0
lines changed

5 files changed

+600
-0
lines changed

session_handler.php

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
<?php
2+
3+
require_once 'session_handler_interface.php';
4+
5+
/**
6+
* Database Session Hander
7+
*
8+
* @author Brett Millett <bmillett@olwm.com>
9+
* @version 1.0
10+
*/
11+
class SessionHandler implements SessionHandlerInterface {
12+
13+
public $table = 'session_handler';
14+
15+
protected $dbh = NULL;
16+
protected $session_id = NULL;
17+
protected $session_written = FALSE;
18+
19+
const ADMIN_EMAIL = 'it@olwm.com';
20+
21+
/**
22+
* Automatically sets this instance to database session handler.
23+
*
24+
* @param PDO $db A PDO instance.
25+
*/
26+
public function __construct(PDO $db) {
27+
$this->dbh = & $db;
28+
29+
// Register this object as the session handler
30+
session_set_save_handler(
31+
array(&$this, 'open'), array(&$this, 'close'),
32+
array(&$this, 'read'), array(&$this, 'write'),
33+
array(&$this, 'destroy'), array(&$this, 'gc')
34+
);
35+
36+
// the following prevents unexpected effects when using objects as save handlers
37+
register_shutdown_function('session_write_close');
38+
39+
session_start();
40+
}
41+
42+
/**
43+
* @return boolean
44+
*/
45+
public function close() {
46+
/**
47+
* Keep session alive with db update where we will call this each method
48+
* each time via session_write_close. This may not be neccessary.
49+
*/
50+
if (!empty($this->session_id) && !$this->session_written) {
51+
try {
52+
$stmt = $this->prepare('UPDATE `%s` SET `timestamp` = NOW() WHERE `id` = ?');
53+
$stmt->execute(array($session_id));
54+
} catch (PDOException $e) {
55+
$this->email_admins($e->getMessage());
56+
} catch (Exception $e) {
57+
$this->email_admins($e->getMessage());
58+
}
59+
}
60+
return TRUE;
61+
}
62+
63+
/**
64+
*
65+
* @param string $session_id
66+
* @return boolean
67+
*/
68+
public function destroy($session_id) {
69+
try {
70+
$stmt = $this->prepare('DELETE FROM `%s` WHERE `id` = ?');
71+
$stmt->execute(array($session_id));
72+
$destroyed = ($stmt->rowCount() > 0);
73+
if ($destroyed)
74+
$this->session_id = NULL;
75+
return $destroyed;
76+
} catch (PDOException $e) {
77+
$this->email_admins($e->getMessage());
78+
} catch (Exception $e) {
79+
$this->email_admins($e->getMessage());
80+
}
81+
return FALSE;
82+
}
83+
84+
/**
85+
*
86+
* @param string $maxlifetime
87+
* @return boolean
88+
*/
89+
public function gc($maxlifetime) {
90+
try {
91+
$stmt = $this->prepare('DELETE FROM `%s` WHERE `timestamp` < ?');
92+
$stmt->execute(array(time() - intval($maxlifetime)));
93+
return ($stmt->rowCount() > 0);
94+
} catch (PDOException $e) {
95+
$this->email_admins($e->getMessage());
96+
} catch (Exception $e) {
97+
$this->email_admins($e->getMessage());
98+
}
99+
return FALSE;
100+
}
101+
102+
/**
103+
*
104+
* @param string $save_path
105+
* @param string $name
106+
* @return boolean
107+
*/
108+
public function open($save_path, $name) {
109+
if ($this->dbh instanceof PDO)
110+
return TRUE;
111+
return FALSE;
112+
}
113+
114+
/**
115+
*
116+
* @param string $session_id
117+
* @return string
118+
*/
119+
public function read($session_id) {
120+
$this->session_id = $session_id;
121+
try {
122+
$stmt = $this->prepare('SELECT `data` FROM `%s` WHERE id = ?');
123+
$stmt->execute(array($session_id));
124+
$result = $stmt->fetch(PDO::FETCH_OBJ);
125+
return (empty($result)) ? '' : $result->data;
126+
} catch (PDOException $e) {
127+
$this->email_admins($e->getMessage());
128+
} catch (Exception $e) {
129+
$this->email_admins($e->getMessage());
130+
}
131+
return '';
132+
}
133+
134+
/**
135+
*
136+
* @param string $session_id
137+
* @param string $session_data
138+
* @param integer $timestamp
139+
* @return boolean
140+
*/
141+
public function write($session_id, $session_data, $timestamp = 0) {
142+
$this->session_written = TRUE;
143+
try {
144+
$stmt = $this->prepare('REPLACE INTO `%s` VALUES(?, ?, ?)');
145+
$stmt->execute(array($session_id, $session_data, ((int) $timestamp > 0) ? (int) $timestamp : time()));
146+
return ($stmt->rowCount() > 0);
147+
} catch (PDOException $e) {
148+
$this->email_admins($e->getMessage());
149+
} catch (Exception $e) {
150+
$this->email_admins($e->getMessage());
151+
}
152+
return FALSE;
153+
}
154+
155+
protected function email_admins($message) {
156+
mail(self::ADMIN_EMAIL, __CLASS__ . ' Error', $message);
157+
}
158+
159+
protected function prepare($query) {
160+
return $this->dbh->prepare(sprintf($query, $this->table));
161+
}
162+
163+
}
164+
165+
//class
166+
167+
/** PHP MySQL Session Handler
168+
========================
169+
170+
Installation
171+
----------------------------
172+
173+
First you need to create a table in your database:
174+
175+
CREATE TABLE `session_handler` (
176+
`id` varchar(255) NOT NULL,
177+
`data` mediumtext NOT NULL,
178+
`timestamp` int(255) NOT NULL,
179+
PRIMARY KEY (`id`)
180+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
181+
*
182+
*/

session_handler_encrypted.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
require_once 'session_handler.php';
4+
5+
/**
6+
* Database Session Hander
7+
*
8+
* @author Brett Millett <bmillett@olwm.com>
9+
* @version 1.0
10+
*/
11+
class SessionHandlerEncrypted extends SessionHandler {
12+
13+
protected $key = NULL;
14+
15+
/**
16+
*
17+
* @param PDO $db
18+
* @param string $key
19+
*/
20+
public function __construct(PDO $db, $key) {
21+
$this->key = substr($key, 0, 24); //make sure no longer than 24 chars.
22+
parent::__construct($db);
23+
}
24+
25+
/**
26+
*
27+
* @param string $session_id
28+
* @return string
29+
*/
30+
public function read($session_id) {
31+
$data = parent::read($session_id);
32+
return mcrypt_decrypt(MCRYPT_3DES, $this->key, base64_decode($data), MCRYPT_MODE_ECB);
33+
}
34+
35+
/**
36+
*
37+
* @param string $session_id
38+
* @param string $session_data
39+
* @return boolean
40+
*/
41+
public function write($session_id, $session_data) {
42+
$session_data = mcrypt_encrypt(MCRYPT_3DES, $this->key, $session_data, MCRYPT_MODE_ECB);
43+
return parent::write($session_id, base64_encode($session_data));
44+
}
45+
46+
}

session_handler_interface.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* Session handler interface
5+
*
6+
* @author bmillett
7+
*/
8+
interface SessionHandlerInterface {
9+
10+
/**
11+
* @return boolean
12+
*/
13+
public function close();
14+
15+
/**
16+
*
17+
* @param string $session_id
18+
* @return boolean
19+
*/
20+
public function destroy($session_id);
21+
22+
/**
23+
*
24+
* @param string $maxlifetime
25+
* @return boolean
26+
*/
27+
public function gc($maxlifetime);
28+
29+
/**
30+
*
31+
* @param string $save_path
32+
* @param string $name
33+
* @return boolean
34+
*/
35+
public function open($save_path, $name);
36+
37+
/**
38+
*
39+
* @param string $session_id
40+
* @return string
41+
*/
42+
public function read($session_id);
43+
44+
/**
45+
*
46+
* @param string $session_id
47+
* @param string $session_data
48+
* @return boolean
49+
*/
50+
public function write($session_id, $session_data);
51+
}

0 commit comments

Comments
 (0)