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
|
<?php
class sql_session extends sql_row_obj {
protected $table='sessions', $primary_key=array('id'), $columns=array(
'id' => array (
'type' => 'VARCHAR',
'length' => 30,
'not_null' => true,
'default' => ''
),
'user' => array (
'type' => 'INT',
'length' => 10,
'unsigned' => true,
'not_null' => true,
'default' => 0,
'refers_to' => 'users.id'
),
'atime' => array (
'type' => 'INT',
'length' => 10,
'unsigned' => true,
'not_null' => true,
'default' => 0
),
'expire' => array (
'type' => 'INT',
'length' => 10,
'unsigned' => true,
'not_null' => true,
'default' => 0
)
);
// Creates a new session for the user at $S['user'] with a unique id, sends a cookie to the user and returns true for success, false for failure
static function create() {
global $S;
$id=null;
while (!$id) {
$id=randstring(30);
$r=query('SELECT * FROM `sessions` WHERE `id`="'.$id.'"');
if ($r->rowCount()) {
$id=null;
}
}
$S['session']=new sql_session($id, $S['user']->id, time(), $S['conf']['sessionlength']);
debug('setcookie', $S['conf']['cookiename'].'='.$id);
if (setcookie($S['conf']['cookiename'], $S['session']->id, time()+$S['conf']['sessionlength'], $S['cookie_dir'], '', false, true)) {
$S['session']->write();
return true;
} else {
return false;
}
}
}
?>
|