-
Notifications
You must be signed in to change notification settings - Fork 16
/
db.php
51 lines (37 loc) · 987 Bytes
/
db.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
<?php
session_name("s");
session_start();
if (empty($_SESSION['count'])) {
$_SESSION['count'] = 1;
} else {
$_SESSION['count']++;
}
$request_method = $_ENV['REQUEST_METHOD'];
$request_uri = substr($_ENV['REQUEST_URI'], 1);
$content_length = $_ENV['CONTENT_LENGTH'];
$filename = "database";
$db = new Db4();
if ($request_method === 'GET') {
if (!empty($_SESSION[$request_uri])) {
echo $_SESSION[$request_uri];
} else {
$db->open(NULL, $filename, NULL);
$val = $db->get($request_uri);
echo $val;
}
} else if ($request_method === 'PUT') {
$buffer = file_get_contents('php://input');
$_SESSION[$request_uri] = $buffer;
} else if ($request_method === 'DELETE') {
unset($_SESSION[$request_uri]);
$db->open(NULL, $filename, NULL);
$db->del($request_uri);
}
if ($_SESSION['count'] > 100) {
unset($_SESSION['count']);
$db->open(NULL, $filename, NULL, DB_CREATE);
foreach ($_SESSION as $key => $value) {
$db->put($key, $value);
}
}
?>