-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.class.php
More file actions
173 lines (134 loc) · 3.99 KB
/
Session.class.php
File metadata and controls
173 lines (134 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* Sessions be in da database now.
* Written by Bradley Gill
* altered effect (http://alteredeffect.com)
* bradgill@gmail.com
*/
class Session {
// You can play with these
public static $database = null;
public static $ttl = 3600; // 1 hour
private static $cookiename = 'S_SID';
private static $tablename = 'sessions';
// But ignore these lol
private static $_instance;
public $sid;
/**
* Return the current session id.
* @return The session id or 'Unknown'.
*/
public static function id() {
if ( Session::$_instance != null )
return Session::$_instance->sid;
return 'Unknown';
}
/**
* Start a session.
* @param if you specify an $sid you can force it to open a certain session. You don't have to do this.
*/
public static function start( $sid = null ) {
// If there's already a session open, save it.
if ( Session::$_instance != null ) {
Session::$_instance->__destruct();
}
Session::$_instance = new Session( $sid );
}
/**
* Destroy the current session.
*/
public static function destroy() {
if ( Session::$_instance != null ) {
Session::$_instance->destroySession();
}
}
// Nothing to see down here!
// Load a session
public function getSession() {
global $_SESSION;
$query = 'SELECT * FROM ' . Session::$tablename . ' WHERE sid=:sid' ;
try {
$stmt = Session::$database->prepare( $query );
$stmt->execute( array('sid'=>$this->sid) );
if ( $stmt->errorCode() == 0 ) {
$obj = $stmt->fetch(PDO::FETCH_ASSOC);
$_SESSION = json_decode( $obj['data'],true );
} else {
$_SESSION = array();
}
} catch( PDOException $e ) {
// If the table doesn't exist, create it.
if ( $e->getCode() == '42S02' ) {
Session::$database->query( 'CREATE TABLE `'.Session::$tablename.'` (
`sid` VARCHAR( 23 ) NOT NULL ,
`data` TEXT NOT NULL ,
`updated_at` INT NOT NULL ,
`remove_at` INT NOT NULL ,
PRIMARY KEY ( `sid` )
) ENGINE = MYISAM');
return true;
}
}
return ( $stmt->errorCode() == 0 );
}
// Save a session to the db
public function saveSession() {
global $_SESSION;
$data = json_encode( $_SESSION );
$stmt = Session::$database->prepare( 'INSERT INTO ' . Session::$tablename . ' (sid, data, updated_at, remove_at) VALUES ( :sid, :data, :updated_at, :remove_at )'
. 'ON DUPLICATE KEY UPDATE sid=VALUES(sid), data=VALUES(data), updated_at=VALUES(updated_at), remove_at=VALUES(remove_at)' );
try {
$stmt->execute( array(
'sid'=>$this->sid,
'data'=>$data,
'updated_at'=>time(),
'remove_at'=>(time()+Session::$ttl)
));
} catch (PDOException $e) {}
return $stmt->errorCode() == 0;
}
// Delete stale sessions
public function cleanup() {
if ( Session::$database != null )
try {
Session::$database->query( 'DELETE FROM ' . Session::$tablename . ' WHERE remove_at < ' . time() );
} catch (PDOException $e) {}
}
// Delete the current session
public function destroySession() {
if ( Session::$database != null )
try {
$stmt = Session::$database->prepare( 'DELETE FROM ' . Session::$tablename . ' WHERE sid = :sid');
$stmt->execute( array('sid'=>$this->sid) );
} catch (PDOException $e) {}
}
// Constructor
private function __construct( $sid ) {
$this->sid = $sid;
if ( $this->sid == null ) {
// Try to get it from the cookie
if ( is_array( $_COOKIE ) and array_key_exists( Session::$cookiename, $_COOKIE ) ) {
$this->sid = $_COOKIE[Session::$cookiename];
}
}
if ( strlen( $this->sid ) < 4 ) {
$this->sid = uniqid( '', true );
}
// If database is invalid, default to regular sessions
if ( Session::$database != null) {
session_start( $this->sid );
} else {
$this->getSession();
}
// Set cookie
setcookie( Session::$cookiename, $this->sid, time() + Session::$ttl );
$this->cleanup();
}
// Destructor: save the session
public function __destruct() {
if ( Session::$database != null ) {
$this->saveSession();
}
}
}
?>