forked from alexanderpavlovru/DbHttpSession
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbHttpSession2.php
148 lines (147 loc) · 5.57 KB
/
DbHttpSession2.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
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* DBHttpSession
*
* Stores session data in database and transfer data when session is destroy.
* Uses for get users online, user's last activity and last ip (and more information if needed)
*
*
* Add this to component section in config/main.php
*
* 'session' => array (
* 'class' => 'application.components.DbHttpSession',
* 'connectionID' => 'db',
* 'sessionTableName' => 'session',
* 'userTableName' => 'user'
* ),
*
* Session table will be created automatically
*
*
* Add columns to your user table:
* ALTER TABLE user ADD user_id INT(11) NOT NULL, ADD last_ip VARCHAR(100) NOT NULL, ADD last_activity DATETIME NOT NULL
*
*/
class DbHttpSession extends CDbHttpSession
{
public $userTableName = "user";
/**
* Transfer data to user table when session is destroy or delete expired records
*
* @param int user_id
* @param string $last_activity
*/
protected function transferData($user_id, $last_activity)
{
if (empty($user_id)) // skip guests users, otherwise sql will return error
return true;
$db = $this->getDbConnection();
$command = $db->createCommand(
"UPDATE $this->userTableName SET last_activity=\"$last_activity\" WHERE id=$user_id"
);
$command->execute();
return true;
}
/**
* Clear expired records from session table and transfer data to user table from the records
*/
protected function clearOldSessions()
{
$db = $this->getDbConnection();
$time = time();
try {
$command = $db->createCommand("SELECT * FROM $this->sessionTableName WHERE expire<\"$time\"");
$result = $command->queryAll();
foreach ($result as $item) {
$id = $item["id"];
$user_id = $item["user_id"];
$last_activity = $item["last_activity"];
$last_ip = $item["last_ip"];
if (!empty($user_id)) { // skip guests users, otherwise sql will return error
$cmdUpd = $db->createCommand(
"UPDATE $this->userTableName SET last_activity=\"$last_activity\", last_ip=\"$last_ip\" WHERE id=$user_id"
);
}
$cmdDel = $db->createCommand("DELETE FROM $this->sessionTableName WHERE id=\"$id\"");
$cmdDel->execute();
$cmdUpd->execute();
}
} catch (Exception $e) {
//TODO write log
$this->createSessionTable($db, $this->sessionTableName);
}
}
protected function createSessionTable($db, $tableName)
{
parent::createSessionTable($db, $tableName);
$db->createCommand()->addColumn($tableName, 'user_id', 'integer not null');
$db->createCommand()->addColumn($tableName, 'last_activity', 'datetime not null');
$db->createCommand()->addColumn($tableName, 'last_ip', 'string not null');
}
public function openSession($savePath, $sessionName)
{
$db = $this->getDbConnection();
$db->setActive(true);
$this->clearOldSessions();
return true;
}
public function writeSession($id, $data)
{
try {
$expire = time() + $this->getTimeout();
$db = $this->getDbConnection();
if ($db->getDriverName() == 'sqlsrv' || $db->getDriverName() == 'mssql'
|| $db->getDriverName() == 'dblib'
) {
$data = new CDbExpression('CONVERT(VARBINARY(MAX), ' . $db->quoteValue($data) . ')');
}
if ($db->createCommand()->select('id')->from($this->sessionTableName)->where('id=:id', array(':id' => $id))
->queryScalar() === false
) {
//Add needed fields to the queries
$db->createCommand()->insert(
$this->sessionTableName, array(
'id' => $id,
'data' => $data,
'expire' => $expire,
'user_id' => Yii::app()->getUser()->getId(),
'last_activity' => new CDbExpression('NOW()'),
'last_ip' => CHttpRequest::getUserHostAddress(),
)
);
} else {
$db->createCommand()->update(
$this->sessionTableName, array(
'data' => $data,
'expire' => $expire,
'user_id' => Yii::app()->getUser()->getId(),
'last_activity' => new CDbExpression('NOW()'),
'last_ip' => CHttpRequest::getUserHostAddress(),
), 'id=:id', array(':id' => $id)
);
}
} catch (Exception $e) {
$this->createSessionTable($db, $this->sessionTableName);
if (YII_DEBUG) {
echo $e->getMessage();
}
return false;
}
return true;
}
public function destroySession($id)
{
$db = $this->getDbConnection();
$command = $db->createCommand("SELECT user_id, last_activity FROM $this->sessionTableName WHERE id=\"$id\"");
$result = $command->queryRow();
$this->transferData($result['user_id'], $result['last_activity']);
$db->createCommand()
->delete($this->sessionTableName, 'id=:id', array(':id' => $id));
return true;
}
public function gcSession($maxLifetime)
{
$this->clearOldSessions();
return true;
}
}