-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboot.php
More file actions
65 lines (53 loc) · 1.47 KB
/
boot.php
File metadata and controls
65 lines (53 loc) · 1.47 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
<?php
session_start();
function pdo(): PDO
{
static $pdo;
if (!$pdo) {
$config = include 'config/config.php';
// Подключение к БД
$dsn = 'mysql:dbname='.$config['db_name'].';host='.$config['db_host'];
$pdo = new PDO($dsn, $config['db_user'], $config['db_pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return $pdo;
}
function create_users_table(): void {
$stmt = pdo()->prepare("CREATE TABLE IF NOT EXISTS users (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL
);");
$stmt->execute();
}
function create_currencies_table(): void {
$stmt = pdo()->prepare("CREATE TABLE IF NOT EXISTS currency (
NumCode INT(11),
CharCode VARCHAR(3),
Nominal INT(11),
Name VARCHAR(50),
Value FLOAT
);");
$stmt->execute();
}
function flash(?string $message = null)
{
if ($message) {
$_SESSION['flash'] = $message;
} else {
if (!empty($_SESSION['flash'])) { ?>
<div class="alert alert-danger mb-3">
<?=$_SESSION['flash']?>
</div>
<?php }
unset($_SESSION['flash']);
}
}
function check_auth(): bool
{
return !!($_SESSION['user_id'] ?? false);
}
function init_db(): void {
create_currencies_table();
create_users_table();
}