-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.php
78 lines (66 loc) · 2.13 KB
/
auth.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
<?php
require_once 'helpers.php';
require_once 'init.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$required = ['email', 'password'];
$errors = [];
$form = filter_input_array(
INPUT_POST,
[
'email' => FILTER_DEFAULT,
'password' => FILTER_DEFAULT
],
true
);
foreach ($form as $key => $value) {
if (in_array($key, $required) && empty($value)) {
$errors[$key] = "Поле $key надо заполнить";
}
}
$errors = array_filter($errors);
$sql = 'SELECT * FROM users WHERE email = ?';
$stmt = db_get_prepare_stmt($link, $sql, [$form['email']]);
mysqli_stmt_execute($stmt);
$res = mysqli_stmt_get_result($stmt);
// такая запись чтобы привыкнуть
//$user = $res ? mysqli_fetch_array($res, MYSQLI_ASSOC) : null;
if ($res) {
$current_user = mysqli_fetch_array($res, MYSQLI_ASSOC);
} else {
$current_user = null;
}
if (!empty($errors)) {
$content = include_template('auth.php', ['form' => $form, 'errors' => $errors]);
} elseif ($current_user) {
if (password_verify($form['password'], $current_user['password'])) {
$_SESSION['user'] = [
'id' => $current_user['id'],
'name' => $current_user['name']
];
} else {
$errors['password'] = 'Неверный пароль';
}
} else {
$errors['email'] = 'Такой пользователь не найден';
}
if (!count($errors)) {
header("Location: index.php");
exit();
} else {
$content = include_template('auth.php', ['form' => $form, 'errors' => $errors]);
}
} else {
$content = include_template('auth.php');
if (isset($_SESSION['user'])) {
header("Location: index.php");
exit();
}
}
require_once 'session_init.php';
$layout_content = include_template('layout.php', [
'is_auth' => $is_auth,
'current_user_name' => $current_user_name,
'content' => $content,
'title' => 'Дела в порядке'
]);
print($layout_content);