forked from htmlacademy-php/1674445-doingsdone-12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.php
89 lines (74 loc) · 2.28 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
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
session_start();
require ('helpers.php');
$page_title = 'Авторизация';
$user = [];
//get data
$conn = db_connect('doingsdone');
$get_users_query = "SELECT * FROM `users`";
$get_users_query_result = mysqli_query($conn, $get_users_query);
$usersList = mysqli_fetch_all($get_users_query_result, MYSQLI_ASSOC);
// validation
$errors = [];
foreach ($_POST as $key => $value) {
if ($key == 'email') {
$len = strlen($_POST[$key]);
if ($len < 1) {
$errors[$key] = 'Введите email';
}
elseif ($len > 0 && filter_var($value, FILTER_VALIDATE_EMAIL) == false) {
$errors[$key] = 'Введите корректный емайл';
} else {
foreach ($usersList as $item) {
if ($item['email'] == $_POST['email']) {
$user = $item;
}
}
if (!count($user)) {
$errors[$key] = 'Пользователь не найден';
}
}
}
if ($key == 'password') {
$len = strlen($_POST[$key]);
if ($len < 1) {
$errors[$key] = 'Введите пароль';
}
if ($len < 8 and $len > 0) {
$errors[$key] = 'Длина пароля должна быть не менее 8 символов';
}
if (count($user)) {
if (!password_verify($_POST['password'], $user['password'])) {
$errors[$key] = 'неверный пароль';
}
}
}
}
$errors = array_filter($errors);
// auth
if ($_SERVER['REQUEST_METHOD'] == 'POST' && count($errors) === 0) {
$_SESSION['userid'] = $user['id'];
$_SESSION['username'] = $user['name'];
if (isset($_SESSION['userid'])) {
header("Location: index.php"); exit;
}
}
if (isset($_SESSION['userid'])) {
// header("Location: index.php"); exit;
echo ($_SESSION['userid']);
}
//templating
$aside_content = include_template('aside.php', [
'user' => $user,
]);
$main_content = include_template('authMain.php', [
'aside_content' => $aside_content,
'errors' => $errors,
]);
$layout_content = include_template('layout.php', [
'page_title' => $page_title,
'main_content' => $main_content,
'user' => $user,
]);
print($layout_content);
?>