-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_session_inc.php
136 lines (113 loc) · 4.08 KB
/
db_session_inc.php
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
<?php
class DbSessionHandler implements SessionHandlerInterface {
//Global variable used for database connection
private $db = NULL;
// This class contaians the following attribute
private $savePath;
/*
* defining method open
* takes two arguments
* $savePath
* $sessionName
*/
public function open($savePath, $sessionName) {
//specify database parameters to create a connection
$user = 'XXXX';
$pass = 'XXXX';
$dbname = 'XXXX';
$host = '127.0.0.1';
try {
// create a db connection
$this->db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
}catch(PDOException $e){
print "Error: " . $e->getMessage() . '</br>';
die();
}//End of try-catch block
return true;
}// End of open function
/*
* defining method close
* takes no arguments
*/
public function close() {
// destroy db connection
$this->db = NULL;
return true;
}// End of close function
/*
* defining method read
* Takes one arfument
* $id string
*/
public function read($id) {
// prepare sql query
$stmt = $this->db->prepare('SELECT data from sessions WHERE id=":id"');
// specify bind parameter
$stmt->bindParam(':id', $id);
// execute query
$stmt->execute();
//fetch record
$row = $stmt->fetch();
//return data value
return $row['data'];
}//End of read function
/*
* defining write function
* takes two arguments
* $id as String
* $data as text
*/
public function write($id, $data) {
// prepare sql query
$stmt = $this->db->prepare('REPLACE INTO sessions(id,data) VALUES (:id, :data)');
// execute query
$row_affected = $stmt->execute(array(':id'=>$id , ':data'=>$data));
//return data value
return $row_affected;
}// End of write function
/*
* defining destroy funciton
* takes one argument
* $id as string
*/
public function destroy($id) {
// prepare sql query
$stmt = $this->db->prepare('DELETE FROM sessions WHERE id=:id');
// execute query
$row_affected = $stmt->execute(array(':id'=> $id));
//return data value
return $row_affected;
}// End of destroy function
/*
* defining gc function
* takes one argument
* $maxlifetime as timestamp
*/
public function gc($maxlifetime) {
// prepare sql query
$stmt = $this->db->prepare('DELETE FROM sessions WHERE DATE_ADD(last_accessed,INTERVAL :expire SECOND) < NOW()');
// specify bind parameter
$stmt->bindParam(':expire', $maxlifetime);
// execute query
$row_affected = $stmt->execute();
//return data value
return $row_affected;
}// end of gc function
}// End of MySessionHandler
$handler = new DbSessionHandler();
session_set_save_handler(
array($handler, 'open'),
array($handler, 'close'),
array($handler, 'read'),
array($handler, 'write'),
array($handler, 'destroy'),
array($handler, 'gc')
);
// the following prevents unexpected effects when using objects as save handlers
register_shutdown_function('session_write_close');
session_start();
//echo $savePath;
//print_r('okii');
//exit();
// proceed to set and retrieve values by key from $_SESSION
?>