This repository was archived by the owner on Aug 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.php
More file actions
71 lines (71 loc) · 1.62 KB
/
Session.php
File metadata and controls
71 lines (71 loc) · 1.62 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
<?php
/**
* Fake Database. All records are stored in $_SESSION
*/
class DB_Session
{
function __construct ()
{
@session_start();
if (! isset($_SESSION['pk'])) {
$this->install();
}
}
private function pk ()
{
return $_SESSION['pk'] ++;
}
private function find ($id)
{
foreach ($_SESSION['rs'] as $index => $rec) {
if ($rec['id'] == $id) {
return $index;
}
}
return FALSE;
}
function get ($id)
{
$index = $this->find($id);
if ($index === FALSE)
return FALSE;
return $_SESSION['rs'][$index];
}
function getAll ()
{
return $_SESSION['rs'];
}
function insert ($rec)
{
$rec['id'] = $this->pk();
array_push($_SESSION['rs'], $rec);
return $rec;
}
function update ($id, $rec)
{
$index = $this->find($id);
if ($index === FALSE)
return FALSE;
$rec['id'] = $id;
$_SESSION['rs'][$index] = $rec;
return $rec;
}
function delete ($id)
{
$index = $this->find($id);
if ($index === FALSE)
return FALSE;
$record = array_splice($_SESSION['rs'], $index, 1);
return array_shift($record);
}
private function install ()
{
/** install initial data **/
$_SESSION['pk'] = 5;
$_SESSION['rs'] = array(
array('id' => 1, 'name' => 'Jac Wright',
'email' => 'jacwright@gmail.com'),
array('id' => 2, 'name' => 'Arul Kumaran',
'email' => 'arul@luracast.com'));
}
}